Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 1.14 KB

problem-009.md

File metadata and controls

39 lines (30 loc) · 1.14 KB

Optimization lead to 18000x faster result over brute force !

Primitive Pythagoream Triplets

Pythoagorean triplets + gcd(a,b,c) = 1

eg. (2,3,5), (5,12,13) etc

Euler's Formula

All primitive triplets can be generated using following:

a = m^2 - n^2
b = 2mn
c = m^2 + n^2
with m > n > 0

The triplet generated by Euler's formula will be primitive if and only if, gcd(a,b,c) = 1

Every Pythagorean Triplet has a unique representation!

From any Pythagorean triplet you get a primitive one by dividing out the greatest common divisor, so every Pythagorean triplet has a unique representation. a = (m^2 − n^2) * d b = 2mnd c = (m^2 + n^2) * d with m > n > 0, gcd(m, n) = 1 and exactly one of m, n even & d is the greatest common divisor of a, b and c.

Back to the problem

Coming to the problem,we can say (a+b+c = s given):

a + b + c = 2 * m * (m+n) * d
s = 2 * m * (m+n) * d
s = 2 * m * k * d with m < k < 2m & gcd(m, k) = 1

**So, our problem is now reduced to brute force for values of m, k i.e. that are two factors of s/2 such that gcd(m,k) = 1. **

More info: https://www.wikiwand.com/en/Pythagorean_triple#Examples Go Crazy !