File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ # /usr/bin/env python
2
+ # code taken from https://maths.dk/teaching/courses/math357-spring2016/projects/factorization.pdf
3
+
4
+ from gmpy2 import gcd , isqrt
5
+
6
+ def euler (self , n ):
7
+ if n % 2 == 0 :
8
+ return (n / 2 , 2 ) if n > 2 else (2 , 1 )
9
+ end = isqrt (n )
10
+ a = 0
11
+ solutionsFound = []
12
+ firstb = - 1
13
+ while a < end and len (solutionsFound ) < 2 :
14
+ bsquare = n - a ** 2
15
+ if bsquare > 0 :
16
+ b = isqrt (bsquare )
17
+ if (b ** 2 == bsquare ) and (a != firstb ) and (b != firstb ):
18
+ firstb = b
19
+ solutionsFound .append ([int (b ), a ])
20
+ a += 1
21
+ if len (solutionsFound ) < 2 :
22
+ return - 1
23
+ a = solutionsFound [0 ][0 ]
24
+ b = solutionsFound [0 ][1 ]
25
+ c = solutionsFound [1 ][0 ]
26
+ d = solutionsFound [1 ][1 ]
27
+ k = gcd (a - c , d - b )
28
+ h = gcd (a + c , d + b )
29
+ m = gcd (a + c , d - b )
30
+ l = gcd (a - c , d + b )
31
+ n = (k ** 2 + h ** 2 ) * (l ** 2 + m ** 2 )
32
+ return [int (k ** 2 + h ** 2 ) // 2 , int (l ** 2 + m ** 2 ) // 2 ]
33
+
34
+ print (euler (int (sys .argv [1 ])))
You can’t perform that action at this time.
0 commit comments