Enigmatic Code

Programming Enigma Puzzles

Enigma 1614: Letters & products

From New Scientist #2779, 25th September 2010 [link]

Six is the product of two primes; one of those primes is the number of letters that the product contains when written as a word in English. (6 = 3×2, and SIX has 3 letters.)

(1) Which 2-digit integer is likewise the product of two primes, one of which is the number of letters that the product contains when written as a word in English?

(2) What is the smallest such 3-digit integer?

(3) What is the smallest such 4-digit integer?

Allow for AND appearing in all integers greater than 100; e.g. ONE HUNDRED AND ONE has 16 letters.

[enigma1614]

Advertisements

One response to “Enigma 1614: Letters & products

  1. jimrandell 29 December 2011 at 8:49 am

    This Python program includes a routine that converts integers to their representation in English (at least for positive integers less than 1 million). Once that’s written the puzzle is easily solved. The runtime is 32ms.

    The int2words() function is now available in the enigma.py module.

    from enigma import is_prime, factor, printf
    
    numbers = {
      0: 'zero',
      1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine',
      10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 15: 'fifteen', 18: 'eighteen',
    }
    
    tens = {
      1: 'teen', 2: 'twenty', 3: 'thirty', 4: 'forty', 5: 'fifty', 6: 'sixty', 7: 'seventy', 8: 'eighty', 9: 'ninety'
    }
    
    def int2words(n):
      if n in numbers:
        return numbers[n]
      if n < 20:
        return numbers[n % 10] + tens[1]
      if n < 100:
        (d, r) = divmod(n, 10)
        x = tens[d]
        if r == 0: return x
        return x + ' ' + numbers[r]
      if n < 1000:
        (d, r) = divmod(n, 100)
        x = int2words(d) + ' hundred'
        if r == 0: return x
        return x + ' and ' + int2words(r)
      if n < 1000000:
        (d, r) = divmod(n, 1000)
        x = int2words(d) + ' thousand'
        if r == 0: return x
        if r < 100: return x + ' and ' + int2words(r)
        return x + ' ' + int2words(r)
      raise NotImplementedError
    
    # find an d digit number that is the product of two primes
    def solve(d, first=True):
      m = 10 ** d
      for n in range(m // 10, m):
        f = factor(n)
        if len(f) != 2: continue
        if not(is_prime(f[0]) and is_prime(f[1])): continue
        w = int2words(n)
        l = sum(1 for x in w if x.isalpha())
        if l not in f: continue
        printf("{n} = P{f}  #\"{w}\" = {l}")
        if first: return
    
    solve(2, False)
    solve(3)
    solve(4)
    

    Solution: (1) 33; (2) 253; (3) 1007.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: