c++,fractions,simplify,greatest-common-divisor

The problem is that you do all your computations on frac before asking the user to input a fraction — and then you overwrite whatever the user inputs. You need to move this bit: cout << "Enter a fraction" << endl; cin >> frac; much, much higher up....

algorithm,greatest-common-divisor

The Binary GCD algorithm tends to outperform the Euclidean algorithm. The idea is to replace division by subtraction and use gcd(a,b) = gcd(a, b-a) and that if a is odd, and b is even, then gcd(a,b) = gcd(a,b/2) which can be implemented as a simple bit operation. If you are...

java,eclipse,greatest-common-divisor

You could use Scanner from java.util, e.g. Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println(a + " " + b); Or BufferedReader from java.io: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(reader.readLine()); int b = Integer.parseInt(reader.readLine()); System.out.println(a + " " + b);...

c++,algorithm,greatest-common-divisor

Of course this loop is infinite: while ((gcf = small - res) > 0) { cout << "gcf = " << gcf << "\n"; } small and res don't change in the loop, so gcf doesn't either. That loop is equivalent to: gcf = small - res; while (gcf >...

java,performance,greatest-common-divisor

The code you posted is a modification of the binary GCD algorithm. Here is my annotated commentary: private static boolean isCoprime(long u, long v) { // If both numbers are even, then they are not coprime. if (((u | v) & 1) == 0) return false; // Now at least...

list,common-lisp,greatest-common-divisor

If I understand what you want correctly, that's easy enough: (defun nested-gcd (list) (reduce #'gcd list :key (lambda (elt) (if (consp elt) (nested-gcd elt) elt)))) ...

python,rsa,greatest-common-divisor

Where you have if d * e == 1 % z: you seem to want to check "Is d*e equal (mod z) to 1" But what you're doing is performing 1 % z (which gives 1) and checking if d * e is equal to 1. Change it to: if...

math,greatest-common-divisor,lcm

I would calculate this in completely different way: the LCM of {1,...,n} is a product of all primes p[i]<=n, each in power floor(log(n)/log(p[i])). This product is divisible by all numbers up to n, and this is the least such number. Your main trouble is to calculate table of primes then.

list,rotation,prolog,elements,greatest-common-divisor

The gcd(a,b,c) = gcd(gcd(a,b),c). It means that for calculating gcd of more than 2 elements, you can first calculate gcd for two first elements, and then use it as an element in a recursive call list: % GCD calculation for two numbers gcd2(A, 0, A). gcd2(A, B, C) :- A...

java,eclipse,debugging,while-loop,greatest-common-divisor

While loop very well executes if you call that. Just added couple of System.out.println and you can see that it is executing: private int greatestCommmonNumetaror(){ int count = 1; int first; int mod; System.out.println("Outside while: count is "+count+" and this.numerator is "+this.numerator); while(count != this.numerator){// need help with this System.out.println("In...

There are a lot of problems with your code. Broadly classifying - Optimization can be talked about when the issue no.2 gets resolved. Code gives wrong answer. Your algorithm is,summarily, incorrect. Dump this approach and think in terms of "Euler Totient" and elementary dynamic programming. For example, Let's say, the...

c,algorithm,greatest-common-divisor

Just go have a try and got AC. As you said hints only, here's some based on my AC solution and your attempt: Phi function is O(n^2)? really? At least in my code, I do not think it is O(n^2) GCD function is not needed It is a simple math...

algorithm,math,greatest-common-divisor,number-theory,clrs

That pseudocode is not Pollard-rho factorization despite the label. It is one trial of the related Brent's factorization method. In Pollard-rho factorization, in the ith step you compute x_i and x_(2i), and check the GCD of x_(2i)-x_i with n. In Brent's factorization method, you compute GCD(x_(2^a)-x_(2^a+b),n) for b=1,2, ..., 2^a....

java,algorithm,greatest-common-divisor

You can precompute the gcd for each prefix and suffix(let's call it gcdPrefix and gcdSuffix) in O(n * log MAX_A) time(just iterate over your array from left to right and store the current gcd, then do the same thing from right to left). The answer for a (L, R) query...

prolog,greatest-common-divisor

First there is a cut missing. You want that the first clause of bar/3 is executed for =0, and the second clause of bar/3 is executed for <>0. This can be done with a cut: foo(P,Q,R):-L is P/Q,R is P-Q*floor(L). bar(A,0,A) :- !. bar(A,B,C):-foo(A,B,X),bar(B,X,C). But the cut doesn't work correctly,...

python,algorithm,greatest-common-divisor

No. There are O(n*n) pairs in the first iteration, and it is possible for any one of them to have a prime factor in common that is shared nowhere else. So in general this is a O(n*n) amount of necessary work to tell the difference between the number of sets...