Code to recognize repeating numbers.
4 次查看(过去 30 天)
显示 更早的评论
lets say we have a fraction were the denominator is always bigger than the numerator. For example 2/3 or 7/12. How would you create code that identifies the repeating part of the decimal for example. 7/12 is 0.583333333 and 1/2 as 0.5000000
I would like code that returns 7/12 as a= 5 8 b = 3
Further 1/2 a = 5 b = 0
so a would be the non repeating part and b would be the repeating part. Thanks for the help on this one. I'd really appreciate it.
4 个评论
Image Analyst
2015-10-18
Any fraction that is limited to the number of decimal places it can have, as are all numbers in a computer, can be represented as a rational number b/a. How many decimal places do you want to inspect before you decide it's a rational number? Or, equivalently, what is the largest denominator you're willing to accept? Would an answer of 18463878207 / 918298769838690 be okay with you?
Walter Roberson
2015-10-18
How odd, just yesterday I was sent the following in email, which is obviously homework:
--- being quote ---
Hey I would really like your help for this one. I was given this question could you solve it for me please, I would be so grateful.
If i have a function
function [a,b]=periodicfraction(p,q)
How would I write code to express divisions in array form. Where (p/q) where 0<q and a is an array of non repeated decimals and b are the repeated decimals. For example, if p=5 and q=6 p/q would be 0.83333333. From this result a (the non repeated part of the decimal) would be 8 and b the repeating part of the decimal would be 3. so my answer should appear as a=(8) b=(3). [a,b]=periodicfraction(1,2) % 1/2 a = 5 b = 0
as 1/2 is 0.50000 b would equal 0.
A final example would be 119/990= 0.120202020202020
[a,b]=periodicfraction(119,990) % 119/990
a = 1
b = 2 0
回答(1 个)
Image Analyst
2015-10-18
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% If the fraction is within this, say it's a match
tolerance = 0.1;
denominators = 2:1000;
theFraction = .123456; % Whatever number you're examining
% Find potential numerators, rounded to the closest integer:
numerators = round(theFraction * denominators);
% We need to get rid of zeros.
zeroIndexes = numerators == 0;
numerators(zeroIndexes) = [];
denominators(zeroIndexes) = [];
% Now get the ratios of integers.
ratios = numerators ./ denominators;
differences = abs(ratios - theFraction);
subplot(2,1,1);
plot(ratios);
title('Ratios', 'FontSize', fontSize);
grid on;
subplot(2,1,2);
plot(differences);
title('Differences', 'FontSize', fontSize);
grid on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Find the min difference:
[minDifference, indexOfMin] = min(differences)
% Print the fraction that is closest:
message = sprintf('%.8f is approximately equal to %d / %d, which equals %.9f.\nThe difference is %.9f.',...
theFraction, round(numerators(indexOfMin)), denominators(indexOfMin), ...
numerators(indexOfMin)/denominators(indexOfMin), minDifference)
uiwait(helpdlg(message));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!