How can we calculate gcd of a^n+b^n and a-b when a,b,n<=10^9
My logic is that gcd(a^n+b^n,a-b)=gcd(a+b,a-b) for every test case this is working...because
formula for a^n+b^n=(a+b)(.....binomial form....)...is my approach correct
At least its exposing people to some math. When they do it on their own, that is.
Hacker, read the other threads. I think your approach is correct, but to be blunt in a friendly way, I don't care enough to sit down with the math to validate it for you. I did my time in college doing wonky proofs in senior math classes, and got my fill of it for life there. You can either prove it on paper or test it to death to see if it works, or submit it and see if it spits back the wrong answer code. I would just do that... best as I can tell you get full points if it gives the right answer, even if the code is not generally correct...
Really? But he said it was because a^n + b^n = (a+b)(...). That's actually what I thought was wrong. I didn't think farther to check gcd with a - b, but when I do it does seems that gcd(a^n+b^n, a-b) = gcd(a+b, a-b).
I think what they meant was that (a+b)^n = a^n + b^n + (etc), which yes, is quite a weaker statement.
I don't really know how to go about proving that GCD(a^n+b^n, a-b) = GCD(a+b, a-b). Raising the terms of a sum has no consistent pattern on the prime factors.
#include <iostream>
#include <iomanip>
long gcd(long a, long b) {
if (b == 0) return 1; // GCD(X, 0) = 1
while (b != 0) { long t = b; b = a % b; a = t; }
return a;
}
void o(long x) {std::cout << std::setw(4) << std::abs(x) << ' ';}
int main() {
for (long a = 1; a < 100; a++)
for (long b = a; b < 100; b++) {
long g1 = gcd(a*a + b*b, a - b);
long g2 = gcd(a + b, a - b);
if (std::abs(g1) != std::abs(g2)) {
o(a);o(b);o(g1);o(g2);std::cout<<'\n';
}
}
}