Skip to content

Commit a03f5a3

Browse files
Add files via upload
Adding faster method using a reduced version of difference of squares I made up (but may or may not have already been dreamed up by someone else)
1 parent e856bc7 commit a03f5a3

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Reduced_Difference_of_Squares.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import math
2+
import time
3+
# from sympy import nextprime
4+
from decimal import *
5+
# Garrick Larson, Spring 2018
6+
getcontext().prec = 1250 # precision of square root function. lower precision = higher speed, higher precision = higher prime factors
7+
8+
# 1200 digits
9+
factor1 = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005813
10+
factor2 = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005227
11+
12+
# factor1 = 191
13+
# factor2 = 103
14+
# factor1 = 86028157
15+
# factor2 = 90713509
16+
17+
sprime = Decimal((factor1*factor2))
18+
getcontext().prec = math.ceil(Decimal(sprime).log10()*Decimal(1.1)) # precision of square root function. lower precision = higher speed, higher precision = higher prime factors
19+
20+
start_time = time.time()
21+
22+
print()
23+
print('Factor 1: ')
24+
print(factor1)
25+
print()
26+
print('Factor 2: ')
27+
print(factor2)
28+
print()
29+
print('Composite Semiprime: ')
30+
print(sprime)
31+
print()
32+
33+
sq = math.ceil(Decimal(sprime).sqrt())
34+
remainder_orig = sq**2 - sprime
35+
sq2 = sq*2 + 1
36+
remainder = remainder_orig
37+
# print(sq2)
38+
# print(remainder)
39+
count = 1
40+
41+
r = Decimal(remainder).sqrt()
42+
while r != (r//1):
43+
remainder += sq2
44+
sq2 += 2
45+
count += 1
46+
if count % 1000 == 0:
47+
print(count)
48+
r = Decimal(remainder).sqrt()
49+
50+
# print("remainder: " + str(remainder) + "\n")
51+
# print("r: " + str(r) + "\n")
52+
# print("sq2: " + str(sq2) + "\n")
53+
54+
55+
56+
difference = r
57+
factor1_output = Decimal(sprime+difference**2).sqrt() + difference
58+
factor2_output = Decimal(sprime+difference**2).sqrt() - difference
59+
60+
print()
61+
print("Factor 1 Output:")
62+
print(factor1_output)
63+
print()
64+
print("Factor 2 Output:")
65+
print(factor2_output)
66+
print()
67+
print("Composite: ")
68+
print(factor1_output*factor2_output)
69+
print()
70+
print("count: " + str(count) + "\n")
71+
72+
print("--- %s seconds to run ---" % (time.time() - start_time))

0 commit comments

Comments
 (0)