View Full Version: Minimum sums

Dozensonline > Number Bases > Minimum sums



Title: Minimum sums


Dan - December 3, 2006 01:02 AM (GMT)
QUOTE
Using all the single digits once only, and excluding 0, what's the minumum [sic] sum you can make when the numbers to be added are all prime?


It then gives answers for bases 5, 6, 7, 8, and :A. Here are a few more:

Base 2: Impossible because 1 is not prime.

Base 3: 12 (5)

Base 4: 2+13=21 (2+7=9)

Base 9: 2+5+18+34+67=2+7+18+34+65=138 (2+5+17+31+61=2+7+18+31+59=116)

Base 11: 2+7+18+3A+49+56=159 (2+7+19+43+53+61=185)

Dan - December 3, 2006 02:08 AM (GMT)
And in dozenal, we have:

2+3+61+85+AB+497
=2+3+61+95+A7+48B
=2+3+67+8B+91+4A5
=2+3+67+91+AB+485
=2+3+67+95+AB+481
=2+3+6B+87+91+4A5
=2+3+6B+91+A7+485
=2+3+6B+95+A7+481
=2+3+81+95+A7+46B
=2+3+85+91+A7+46B
=2+3+87+91+AB+465
=2+3+8B+91+A7+465
=6B5 (dec. 1001)

Dan - December 3, 2006 05:58 PM (GMT)
Here's a program that finds the minimum sum. (Sorry about the variable naming.)

CODE
#!/usr/bin/env python

"""minimum sum solver

This program answers the question: Using all the single digits once
only, and excluding 0, what's the minimum sum you can make when the
numbers to be added are all prime?

Usage: minimum_sum.py radix [max_prime]
max_prime is the upper bound for prime numbers.  Default is 10000.
radix must be an integer between 3 and 36.
"""

import baseconv

itoa = baseconv._itoa

def primes(upper_bound):
   """Return a list of all primes in [2, upper_bound)."""
   # Sieve of Eratosthenes
   result = []
   potential = range(2, upper_bound)
   while potential:
       next_prime = potential[0]
       result.append(next_prime)
       potential = [num for num in potential if num % next_prime]
   return result

def unique_digits(string):
   """Return True if string has no repeated characters, false otherwise."""
   return len(string) == len(set(string))

def sums(base, max_prime):
   """
   Generates tuples, each of which partitions the digits 1 to base-1
   into prime numbers in the given base.
   max_prime = upper bound for the prime numbers to use
   """
   PRIMES = [itoa(num, base) for num in primes(max_prime)]
   PRIMES = [num for num in PRIMES if '0' not in num and unique_digits(num)]
   sum_list = [[(num,) for num in PRIMES]]
   while True:
       # Add an additional prime number to each sum
       new_sums = []
       for sum_ in sum_list[-1]:
           for prime in PRIMES:
               new_sum = sum_ + (prime,)
               if unique_digits(''.join(new_sum)):
                   new_sums.append(new_sum)
       if not new_sums:
           break
       # Save sums, removing duplicates
       new_sums = map(tuple, set(map(frozenset, new_sums)))
       sum_list.append(new_sums)
   # Yield sums with the correct number of digits
   for sum_list_2 in sum_list:
       for sum_ in sum_list_2:
           if len(''.join(sum_)) == base - 1:
               yield sum_

def minimum_sums(base, max_prime):
   """Yield strings containing the minimum prime sum."""
   sum_list = list(sums(base, max_prime))
   converted_sums = [[int(num, base) for num in sum_] for sum_ in sum_list]
   min_sum = min(sum(prime_list) for prime_list in converted_sums)
   result = [sorted(prime_list) for prime_list in converted_sums
             if sum(prime_list) == min_sum]
   result.sort()
   min_sum = itoa(min_sum, base)
   for sum_ in result:
       sum_ = [itoa(num, base) for num in sum_]
       yield '%s=%s' % ('+'.join(sum_), min_sum)

def _main(argv=None):
   """Executed when this module is run as a script."""
   import sys
   if argv is None:
       argv = sys.argv
   if len(argv) == 2:
       base = int(argv[1])
       max_prime = 10000
   elif len(argv) == 3:
       base = int(argv[1])
       max_prime = int(argv[2])
   else:
       print >> sys.stderr, __doc__
   for sum_ in minimum_sums(base, max_prime):
       print sum_

if __name__ == '__main__':
   _main()


Dan - December 3, 2006 06:10 PM (GMT)
I found better results for bases 7 and 8.

Base 3:
12=12

Base 4:
2+13=21

Base 5:
12+34=101

Base 6:
3+21+45=113

Base 7:
2+5+16+43=102

Base 8:
2+3+65+147=241

Base 9:
2+5+18+34+67=138
2+7+18+34+65=138

Base 10:
2+3+5+41+67+89=207
2+3+5+47+61+89=207
2+5+7+43+61+89=207

Base 11:
2+7+18+3A+49+56=159

Base 12:
2+3+61+85+AB+497=6B5
2+3+61+95+A7+48B=6B5
2+3+67+8B+91+4A5=6B5
2+3+67+91+AB+485=6B5
2+3+67+95+AB+481=6B5
2+3+6B+87+91+4A5=6B5
2+3+6B+91+A7+485=6B5
2+3+6B+95+A7+481=6B5
2+3+81+95+A7+46B=6B5
2+3+85+91+A7+46B=6B5
2+3+87+91+AB+465=6B5
2+3+8B+91+A7+465=6B5

Base 13:
2+B+1A+38+49+56+7C=1B6

Base 14:
2+3+7+45+6D+81+A9+CB=319
2+3+7+4B+65+81+A9+CD=319
2+3+7+4B+6D+81+A9+C5=319
2+5+7+43+6D+81+A9+CB=319
2+7+B+43+65+81+A9+CD=319
2+7+B+43+6D+81+A9+C5=319
2+7+D+43+65+81+A9+CB=319

Base 15:
2+5+14+38+67+9E+AD+CB=304
2+5+14+38+6B+9E+A7+CD=304
2+5+14+38+6D+9E+A7+CB=304
2+5+18+3E+67+94+AD+CB=304
2+5+18+3E+6B+94+A7+CD=304
2+5+18+3E+6D+94+A7+CB=304
2+5+1E+38+67+94+AD+CB=304
2+5+1E+38+6B+94+A7+CD=304
2+5+1E+38+6D+94+A7+CB=304




Hosted for free by InvisionFree