Code to recognize repeating numbers.

조회 수: 4 (최근 30일)
Max
Max 2015년 10월 18일
답변: Image Analyst 2015년 10월 18일
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
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?
This sounds so much like homework. If it is, read this
Walter Roberson
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
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));

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by