Problem 123

completed January 20, 2012

Comments

Another quick problem. I used my usual ‘isprime’ function, but added another, called ‘primes’. This one found the next prime after the one given. This way, I didn’t have to generate a huge number of primes up front and dig through those. Doing it that way would have meant holding onto a huge list which would have slowed things down. This worked out great, though I wish it would have run a little faster.

Code

def isprime(n):
	if n == 0 or n == 1: return False
	for i in range(2, int(n**0.5 + 1)):
		if n%i == 0: return False
	return True

def primes(p):
	while True:
		p += 1
		if isprime(p) == True:
			return p

n = 0; p = 0
while True:
	n += 1
	p = primes(p)
	if n%100 == 0: print n, p
	r = ((p-1)**n + (p+1)**n) % p**2
	if r > 10**10:
		print n, 'final answer'
		break