rational-numbers

simplifying fractions in Java

会有一股神秘感。 提交于 2019-11-29 05:32:09
My task is to develop a rational class. If 500 and 1000 are my inputs, then (½) must be my output. I have written a program on my own to find it. Is there another best way to find the solution, or my program is already the best one? public class Rational { public static void main(String[] args){ int n1 = Integer.parseInt(args[0]); int n2 = Integer.parseInt(args[1]); int temp1 = n1; int temp2 = n2; while (n1 != n2){ if(n1 > n2) n1 = n1 - n2; else n2 = n2 - n1; } int n3 = temp1 / n1 ; int n4 = temp2 / n1 ; System.out.print("\n Output :\n"); System.out.print(n3 + "/" + n4 + "\n\n" ); System.exit

Algorithm for detecting repeating decimals?

半腔热情 提交于 2019-11-29 02:02:46
Is there an algorithm for figuring out the following things? If the result of a division is a repeating decimal (in binary). If it repeats, at what digit (represented as a power of 2) does the repetition start? What digits repeat? Some examples: 1/2 = 1/10 = 0.1 // 1 = false, 2 = N/A, 3 = N/A, 4 = N/A 1/3 = 1/11 = 0.010101... // 1 = true, 2 = -2, 3 = 10 2/3 = 10/11 = 0.101010... // 1 = true, 2 = -1, 3 = 10 4/3 = 100/11 = 1.010101... // 1 = true, 2 = 0, 3 = 10 1/5 = 1/101 = 0.001100110011... // 1 = true, 2 = -3, 3 = 1100 Is there a way to do this? Efficiency is a big concern. A description of

The “guess the number” game for arbitrary rational numbers?

只谈情不闲聊 提交于 2019-11-28 15:01:13
I once got the following as an interview question: I'm thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) queries. Each query is a number of your choosing, and I will answer either "lower," "higher," or "correct." This problem can be solved by a modified binary search, in which you listing powers of two until you find one that exceeds n, then run a standard binary search over that range. What I think is so cool about this is that you can search an infinite space for a particular number faster than just brute-force. The question I have, though, is a slight

Why do Haskell numerical literals need to start and end with digits?

一个人想着一个人 提交于 2019-11-28 09:20:22
问题 In The Haskell 98 Report it's said that A floating literal must contain digits both before and after the decimal point; this ensures that a decimal point cannot be mistaken for another use of the dot character. What other use might this be? I can't imagine any such legal expression. (To clarify the motivation: I'm aware that many people write numbers like 9.0 or 0.7 all the time without needing to, but I can't quite befriend myself with this. I'm ok with 0.7 rather then the more compact but

Converting float decimal to fraction

▼魔方 西西 提交于 2019-11-27 20:44:48
I am trying to convert calculations keyed in by users with decimal results into fractions. For e.g.; 66.6666666667 into 66 2/3. Any pointers? Thanx in advance Joni Continued fractions can be used to find rational approximations to real numbers that are "best" in a strict sense. Here's a PHP function that finds a rational approximation to a given (positive) floating point number with a relative error less than $tolerance : <?php function float2rat($n, $tolerance = 1.e-6) { $h1=1; $h2=0; $k1=0; $k2=1; $b = 1/$n; do { $b = 1/$b; $a = floor($b); $aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux; $aux = $k1

Convert a decimal number to a fraction / rational number

本秂侑毒 提交于 2019-11-27 14:40:00
In JavaScript, is there any way to convert a decimal number (such as 0.0002 ) to a fraction represented as a string (such as " 2/10000" )? If a function called decimalToFraction had been written for this purpose, then decimalToFraction(0.0002) would return the string "2/10000" . You can use Erik Garrison's fraction.js library to do that and more fractional operations. var f = new Fraction(2, 10000); console.log(f.numerator + '/' + f.denominator); To to do .003 you can just do var f = new Fraction(.003); console.log(f.numerator + '/' + f.denominator); Alex Wayne A little googling with the term

The “guess the number” game for arbitrary rational numbers?

时间秒杀一切 提交于 2019-11-27 08:58:09
问题 I once got the following as an interview question: I'm thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) queries. Each query is a number of your choosing, and I will answer either "lower," "higher," or "correct." This problem can be solved by a modified binary search, in which you listing powers of two until you find one that exceeds n, then run a standard binary search over that range. What I think is so cool about this is that you can search an

Converting float decimal to fraction

寵の児 提交于 2019-11-26 20:25:56
问题 I am trying to convert calculations keyed in by users with decimal results into fractions. For e.g.; 66.6666666667 into 66 2/3. Any pointers? Thanx in advance 回答1: Continued fractions can be used to find rational approximations to real numbers that are "best" in a strict sense. Here's a PHP function that finds a rational approximation to a given (positive) floating point number with a relative error less than $tolerance : <?php function float2rat($n, $tolerance = 1.e-6) { $h1=1; $h2=0; $k1=0;

Convert a decimal number to a fraction / rational number

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:50:51
问题 In JavaScript, is there any way to convert a decimal number (such as 0.0002 ) to a fraction represented as a string (such as " 2/10000" )? If a function called decimalToFraction had been written for this purpose, then decimalToFraction(0.0002) would return the string "2/10000" . 回答1: You can use Erik Garrison's fraction.js library to do that and more fractional operations. var f = new Fraction(2, 10000); console.log(f.numerator + '/' + f.denominator); To to do .003 you can just do var f = new