import sys
import time
# Remove the limit
sys.set_int_max_str_digits(0)
i = 0
def signal_handler(sig, frame):
global i
if sig == signal.SIGINT:
print("index = %i" % (i))
sys.exit(0)
def int_sqrt(n):
"""
Calculate the integer square root of a non-negative integer n.
This function returns the largest integer whose square is less than or equal to n.
"""
if n < 0:
raise ValueError("Cannot calculate square root of a negative number.")
if n == 0:
return 0
x = n
y = 1
i = 0
while x > y:
x = (x + y) // 2
y = n // x
return x
def prime_power(n):
signal.signal(signal.SIGINT, signal_handler)
total = 0
half_n = (n + 1) // 2
# limit = half_n
# limit = math.ceil((half_n*(half_n + 1) // 2 + 1) / n)
limit = (n + 13)//8
start_time = time.perf_counter()
end_time = start_time
global i
for i in range(2, limit):
# border_value represents the number that ends on the last
# column when all numbers up to it are added and wrapped.
border_value = int((int_sqrt(8 * i * n + 1) - 1)/2)
total = (border_value * (border_value + 1)//2)
elapsed_time = end_time - start_time
if (total == (i * n)):# or (total + (border_value + 1)//2 == (i * n)):
print("%i" % (border_value))
return
end_time = time.perf_counter()
elapsed_time = end_time - start_time
if elapsed_time > WAIT_TIME:
start_time = end_time
end_time = time.perf_counter()
print("\tprime_power")
print("p\tborder\tprime power")
prime_power(2**89-1)
Saturday, April 26, 2025
My Mersenne Primality Testing Algorithm
I'm not sure how fast it is compared to GIMPS, but it is acurate for all Mersenne or Fermat primes. See my article Value-Counting Up to N for why.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment