Example #1
0
def goesTo89 (num) :

	if num in d :
		return d[num]
	ret = goesTo89(sum(map(lambda x : x*x,digits(num))))
	d[num] = ret
	return ret
Example #2
0
def isCircular(n):
    try:
        return circulars[n]
    except:
        if 0 in digits(n):
            return False
        s = str(n)
        num = int(s[1:] + s[0])
        numList = [num]
        while num != n:
            s = str(num)
            num = int(s[1:] + s[0])
            numList.append(num)
        circular = True
        for i in numList:
            if not isPrime.setdefault(i, False):
                circular = False
                break
        for i in numList:
            circulars[i] = circular
        return circular
Example #3
0
m = {}
squares = {}
lengths = {}

for word in words:
	w = list(word)
	w.sort()
	w = tuple(w)
	if w in m :
		m[w].append(word)
	else :
		m[w] = [word]

for i in range(10**5) :
	sq = i**2
	digs = digits(sq)
	if len(digs) == len(set(digs)) :
		digs = len(digs)
		if digs in squares :
			squares[digs].add(sq)
		else :
			squares[digs] = set([sq])

for key in m :
	if len(m[key]) > 1 :
		l = len(m[key][0])
		if l in lengths :
			lengths[l].append(m[key])
		else :
			lengths[l] = [m[key]]
Example #4
0
				l = [firstDig]
				l.extend(lis)
				tabs.append(l)
		return tabs

totalSum = 0

for repeatedDigit in range(10) :
	primes = set([])
	for numRepeats in range(numlength-1,0,-1) :
		numUniques = numlength - numRepeats
		print repeatedDigit, numRepeats
		primeCount = 0
		primeSum = 0
		for i in range(10**numUniques) :
			uniqueDigs = digits(i,numUniques)
			for combo in genAllLists(numUniques,0,9) :
				digs = [repeatedDigit]*numRepeats
				for place in reversed(range(len(combo))) :
					digs[combo[place]:combo[place]] = [uniqueDigs[place]]
				if digs[0] == 0 :
					continue
				num = digitsToNum(digs)
				if num not in primes and isPrime(num) :
					#print num
					primes.add(num)
					primeCount += 1
					primeSum += num
		if primeCount > 0 :
			print "S(", numlength, ", ", repeatedDigit, ") =", primeSum
			totalSum += primeSum
Example #5
0
from useful import digits
from math import log10

i = 2
lis = []
try :
	while (True) :
		prod = i
		while (log10(prod) < i) :
			if (sum(digits(prod)) == i) :
				lis.append(prod)
			prod *= i
		i += 1
except KeyboardInterrupt, e :
	print sorted(lis)
Example #6
0
			ret.append(p)
			n /= p
		if n == 1 :
			return ret
	return ret + [n]

def phi(n) :
	facts = primeFactorize(n)
	if len(facts) == 1:
		return n-1
	ret = n
	for fact in facts :
		ret /= fact
		ret*= fact-1
	return ret

minVal = 100
for num in xrange(10**7,1,-1) :
	ph = phi(num)
	if float(num)/ph < minVal and int(log10(ph)) == int(log10(num)) :
		digsp = digits(ph)
		digsn = digits(num)
		for dig in digsp :
			if dig in digsn :
				digsn.remove(dig)
			else :
				break
		else :
			print num
			minVal = float(num)/ph
			print minVal
Example #7
0
# Find the only eleven primes that are truncatable from left to right and from right to left

from useful import digits, primeList, digitsToNum

isPrime = {}
primes = primeList(1000000)

sum = 0
for p in primes :
	isPrime[p] = True
	dList = digits(p)
	satisfies = True
	for i in range(1,len(dList)) :
		if not isPrime.setdefault(digitsToNum(dList[i:]),False) :
			satisfies = False
			break
	for i in range(1,len(dList)) :
		if not isPrime.setdefault(digitsToNum(dList[:i]),False) :
			satisfies = False
			break
	if satisfies :
		print p
		sum += p

print sum
		
Example #8
0
# Compute the irrational square roots of all n in 1 <= n <= 100 to 100 decimal places
	# The answer is the sum of all the decimal digits
	
from math import log10, sqrt
from useful import digits	

s = 0

for square in range(100) :
	if square in [0,1,4,9,16,25,36,49,64,81] :
		continue
	c = square
	p = 0
	x = int(sqrt(square))
	y = (20*p + x)*x
	p = x
	c = c - y
	for times in range(99) :
		c *= 100
		for x in range(11) :
			y = (20*p + x)*x
			if y > c :
				x -= 1
				y = (20*p + x)*x
				p = 10*p + x
				break
		c = c - y
	s += sum(digits(p))

print s
Example #9
0
# Find the maximum possible sum of digits for a^b, with a,b < 100

from useful import digits

maxA, maxB, maxSum = 0,0,0

for a in range (100) :
	for b in range(100) :
		s = sum(digits(a**b))
		maxSum = max([s,maxSum])
		if s == maxSum :
			maxA = a
			maxB = b

print maxSum, a, b
Example #10
0
answer = 0

def get_sol(a,b,c) :
  (x, y) = extended_gcd(a,b)
  return (x*c, y*c)

def extended_gcd(a,b) :
  if b == 0 :
    return (1,0)
  q = a / b
  r = a % b
  (s, t) = extended_gcd(b, r)
  return (t, s - q*t)

for i in xrange(len(primes)) :
  p1 = primes[i]
  p2 = primes[i+1]
  if p1 > 1000000 :
    break
  digs = len(digits(p1))
  a = -(10**digs)
  b = p2
  c = p1
  (solx, soly) = get_sol(a,b,c)
  n = int(soly/a) + 1
  realy = soly - a * n
  solution = p2 * realy
  answer += solution

print answer
Example #11
0
# Find the 1th,10th,100th,...,1000000th digits of the decimal 
# 0.1234567891011121314...

count = 1
num = 1
ret = []

from math import log10
from useful import digits

while (count <= 1000001) :
	for dig in digits(num) :
		if count in [1,10,100,1000,10000,100000,1000000] :
			ret.append(dig)
		count += 1
	num += 1

print ret
Example #12
0
cands1 = set([frozenset([3])])
cands2 = set([frozenset([3])])

def makesPrimes(a,b) :
	if isPrime(a * 10**(int(log10(b))+1) + b) and isPrime(b * 10**(int(log10(a))+1) + a) :
		return True
	return False

count = 0

#Reconnaissance
for p in primes[2:] :
	#print p
	toAdd = set([])
	toRemove = set([])
	pool = sum(digits(p)) % 3
	if pool == 1 :
		targetSet = cands1
	else :
		targetSet = cands2
	for cand in targetSet :
		works = set([])
		for prime in cand :
			if makesPrimes(prime,p) :
				works.add(prime)
		if len(works) == len(cand) :
			toRemove.add(cand)
			toAdd.add(frozenset(cand | set([p])))
		else :
			toAdd.add(frozenset(works | set([p])))
		if len(cand) == search :
Example #13
0
#Find the first Fibonacci number whose first and last nine digits are pandigital

from useful import digits

last1 = 1
last2 = 1
first1 = 1
first2 = 1
term = 3
lotsOfDigs = 10**15
while(True):
	lastCurr = (last1 + last2) % 10**9
	firstCurr = (first1 + first2)
	if firstCurr >= lotsOfDigs :
		firstCurr /= 10
		first2 /= 10
	last9 = digits(lastCurr)
	first9 = digits(firstCurr)[:9]
	print term, firstCurr
	if 0 not in first9 and len(first9) == 9 and len(set(first9)) == 9 and 0 not in last9 and len(last9) == 9 and len(set(last9)) == 9:
		print term
		break
	last1 = last2
	last2 = lastCurr
	first1 = first2
	first2 = firstCurr
	term += 1
Example #14
0
# Find the sum of the digits in the numerator of the 100th convergent of e

from useful import digits

def convergent(n) :
	if n == 1 :
		return (2,1)
	def recurseUpToN(step) :
		if step % 3 == 0 :
			val = 2*(step/3)
		else :
			val = 1
		if step == n :
			return (1,val)
		else :
			next = recurseUpToN(step+1)
			tup = (val * next[1] + next[0], next[1])
			return (tup[1],tup[0])
	ret = recurseUpToN(2)
	return (2 * ret[1] + ret[0],ret[1])

tup = convergent(100)
print tup

print sum(digits(tup[0]))
Example #15
0
# Find the largest number that can be produced by taking an integer and
# multiplying it by numbers 1..n such that the concatenated products form
# a 9-pandigital

from math import log10
from useful import digits

maxNum = 0

for i in range(10, 10000):
    num = i
    count = 2
    while num < 10 ** 8:
        newDigs = i * count
        num *= 10 ** int(log10(newDigs) + 1)
        num += newDigs
        count += 1
    digs = digits(num)
    if len(digs) == 9 and len(set(digs)) == 9 and 0 not in digs:
        print i, count, num
        maxNum = max((maxNum, num))

print maxNum
Example #16
0
# Find the sum of all 0 to 9 pandigital primes where
# d2-4 is divisible by 2, d3-5 is divisible by 3, d4-6 is divisible by 5
# and so on through the primes

from useful import digits, primeList, digitsToNum

primes = primeList(18)
sum = [0]

def satisfies(threeDigs, primeIndex, allDigsSoFar) :
	if primeIndex == 0 :
		if threeDigs % 2 == 0 and len(set(allDigsSoFar)) == 9 :
			count = 0
			while (count < 10)  :
				if count not in allDigsSoFar :
					break
				count += 1
			val = digitsToNum([count] + allDigsSoFar)
			print val
			sum[0] += val
	lastTwoDigs = threeDigs/10
	for i in range(10) :
		if (i * 100 + lastTwoDigs) % primes[primeIndex-1] == 0 and i not in allDigsSoFar :
			satisfies(i*100+lastTwoDigs, primeIndex-1, [i] + allDigsSoFar)

for j in range(6,59) :
	digs = digits(j*17,3)
	if len(set(digs)) == 3:
		satisfies(j*17,6,digs)

print sum
Example #17
0
d[3041631117224635] = 3
d[1841236454324589] = 3
d[2659862637316867] = 2

"""
d[90342] = 2
d[70794] = 0
d[39458] = 2
d[34109] = 1
d[51545] = 2
d[12531] = 1
"""

digits_map = {}
for key in d :
  digits_map[key] = digits(key)

seq = [set([0,1,2,3,4,5,6,7,8,9]) for x in xrange(16)]
keys = []

# Build the list of keys, with keys with fewer correct entries
# coming first
for key in d :
  if d[key] == 0 :
    digs = digits_map[key]
    for i in xrange(len(digs)) :
      seq[i].discard(digs[i])
  elif d[key] == 1 :
    keys.append(key)
for key in d :
  if d[key] == 2 :
Example #18
0
	def numify(self,lis) :
		ret = []
		for l in lis :
			ret.extend(digits(l))
		return digitsToNum(ret)
Example #19
0
# Find the number of Lychrel numbers below 10000

from useful import digits, isPali, digitsToNum

count = 0
for i in range(10000) :
	sum = i
	for j in range(50) :
		l = digits(sum)
		l.reverse()
		sum += digitsToNum(l)
		if isPali(sum) :
			break
	else :
		print i
		count += 1

print count
		
Example #20
0
# Find the only other 3-term, 4-digit arithmetic sequence where all terms are prime and all terms
# contain the same digits
# One is 1487, 4817, 8147

from useful import digits, primeList

primes = primeList(10000)
nums = {}

for p in primes :
	digs = digits(p)
	digs.sort()
	dT = tuple(digs)
	val = nums.setdefault(dT, [0,[]]) 
	val[0] += 1
	val[1].append(p)
	nums[dT] = val

for k in nums.keys() :
	if nums[k][0] >= 3 :
		this = nums[k][1]
		this.sort()
		for i in range(len(this)) :
			for j in range(i+1,len(this)) :
				for k in range(j+1, len(this)) :
					if this[j] - this[i] == this[k] - this[j] :
						print this[i], this[j], this[k]
						

Example #21
0
# Find all numbers x that can be factored into x=ab such that x, a, and b contain each digit 1 through 9 exactly once.

# Let us generate all 4 or 5-digit numbers that are composed of unique digits.

from useful import primeFactorize, digits
				
sum = 0
for i in range (1000, 10000) :
	dList = digits(i)
	if (len(set(dList)) != len(dList) or 0 in dList) :
		continue
	factors = primeFactorize(i)
	for j in range(2**len(factors)) :
		num1 = 1
		num2 = 1
		for k in range(len(factors)) :
			if j/(k+1) == 1 :
				num1 *= factors[k]
			else :
				num2 *= factors[k]
		allDigits = dList + digits(num1) + digits(num2)
		if (len(allDigits) == 9 and len(set(allDigits)) == 9 and 0 not in allDigits) :
			print i, num1, num2, "!"
			sum += i
			break

print sum
Example #22
0
# Find the smallest cube for which 5 permutations of its digits are cubes

perms = {}

from useful import digits

count = 1
while True :
	num = count**3
	cub = digits(num)
	cub.sort()
	cub = tuple(cub)
	l = perms.setdefault(cub,[0,num])
	l[0] += 1
	if l[0] >= 5 :
		print l
		break
	perms[cub] = l
	count += 1