I wanted to write a program that tried to look for these primes. I have run it a few times and when N gets to 25 it does not have an output in over 24 hours. Is this just because it will take a while or because something is wrong with it?
Here is my code:
import time
def isprime(n):
"""
Assumes that n is a positive natural number
"""
if n % 2 == 0:
return False
i = 3
# This will loop from 2 to int(sqrt(x))
while i*i <= n:
# Check if i divides x without leaving a remainder
if n % i == 0:
# This means that n has a factor in between 2 and sqrt(n)
# So it is not a prime number
return False
i += 2
# If we did not find any factor in the above loop,
# then n is a prime number
return True
prime = False
primes = []
n = 0
while n <=1000:
startTime = time.time()
num = ''
n += 1
for i in range(1,n+1):
num += str(i)
for j in reversed(range(1,n)):
num += str(j)
prime = isprime(int(num))
if prime:
primes.append(num)
prime = False
print(f"N{n} took {time.time()- startTime} secounds")
print(primes)
Credit for the isprime
function:
https://www.rookieslab.com/posts/fastest-way-to-check-if-a-number-is-prime-or-not