- Location
- ...Nice try.
find the 2^^^^^^^^2th prime number using a very slow algorithm
Bogoprime.
EDIT: source code:
Code:
from random import randint
def bogoprime(n):
def prime_test(m):
prime = True
for i in range(2, arr[n-1]):
if m % i == 0:
if m != i:
prime = False
return prime
def prime_list():
sorted_primes = True
for i in range(n-1):
for j in range(arr[i]+1, arr[i+1]):
if prime_test(j):
sorted_primes = False
if not prime_test(arr[i+1]):
sorted_primes = False
return sorted_primes
arr = bogoarr(n)
while not prime_list():
arr = bogoarr(n)
return arr
def bogoarr(n):
def PI(arr):
product = 1
for i in arr:
product *= i
return product
arr = [2]
for i in range(n-1):
arr.append(randint(arr[i]+1, PI(arr)+1))
return arr
Recursively generate an array of n numbers:
- Base case: [2]
- Inductive step: randomly select a number strictly greater than the last element of the array and at most the product of the elements of the array plus 1 and add it to the array.
Notice the similarity to the classical proof that the primes are unbounded above; this shows that the array may contain the nth prime.
For each entry of the array, test whether it's the next prime:
- For every m between the previous entry and the current entry, test for primality*.
- Test the current entry for primality using the same method
If the array doesn't contain the first n primes and no other entries, rinse, repeat by generating a new array.
*
prime = True
for i in range(2, a[n-1]):
if m % i == 0:
if m != i:
prime = False #edited because I forgot that Python booleans are uppercase
return prime
Last edited: