Enigmatic Code

Programming Enigma Puzzles

Enigma 1021: All at threes and sevens

From New Scientist #2177, 13th March 1999 [link]

For his work in detention, Johnny was set to multiply two large numbers together. One number consisted entirely of threes, the other entirely of sevens:

3333… × 7777… = ???

Surprisingly, he managed to get the correct answer. When he examined his answer he noticed that it contained exactly 7 sevens and 3 threes.

How many digits were there altogether in Johnny’s answer?

[enigma1021]

2 responses to “Enigma 1021: All at threes and sevens

  1. Jim Randell 1 March 2019 at 7:56 am

    We are looking at the product of:

    333…333 × 777…777

    Which we can write as:

    (3 × 111…111) × (7 × 111…111)
    = (3 × 7) × (111…111 × 111…111)

    So we can examine products of repunits to find an appropriate value, which will then correspond to two possible multiplications (if the repunits have different lengths).

    This Python program runs in 93ms.

    Run: [ @repl.it ]

    from itertools import count
    from enigma import nsplit, printf
    
    # generate (a, b, p) pairs, where:
    # a is a repunit
    # b is a repunit, not shorter than a
    # p = a * b
    def generate():
      for t in count(2):
        (a, b) = (1, (10 ** (t - 1) - 1) // 9)
        while not(b < a):
          yield (a, b, a * b)
          (a, b) = (10 * a + 1, b // 10)
    
    # find the first solution to the problem
    for (a, b, p) in generate():
      p *= 21
      ds = nsplit(p)
      if ds.count(3) == 3 and ds.count(7) == 7:
        printf("(3 x 7) x ({a} x {b}) = {p} [{n} digits]", n=len(ds))
        break
    

    Solution: There were 47 digits in Johnny’s answer.

    The possible multiplications are:

    3333333333333333333333 × 7777777777777777777777777 = 25925925925925925925923330740740740740740740741
    3333333333333333333333333 × 7777777777777777777777 = 25925925925925925925923330740740740740740740741

  2. Brian Gladman 1 March 2019 at 1:53 pm
    from itertools import count
    from collections import Counter
    from number_theory import factor
    
    # consider increasing combined lengths of the two numbers
    for mn in count(2):
      for m in range(1, mn):
        # consider different lengths within this combined length
        n = mn - m
    
        # form 33...33 of length m
        rdm = (10 ** m - 1) // 3
        # .. and 77...77 of length n
        rdn = 7 * ((10 ** n - 1) // 9)
        # ... and their product
        prd = str(rdm * rdn)
    
        # check for the specified numbers of 3's and 7's in the product
        if prd.count('3') == 3 and prd.count('7') == 7:
          print(f"3..({m})..3 x 7..({n})..7\n  = {prd} ({len(prd)} digits)")
          
          # factor the number into its prime factors
          f = Counter(factor(rdm)) + Counter(factor(rdn))
          print('  =', '.'.join(f"{p}" if e  == 1 else f"{p}^{e}" for p, e in sorted(f)))
          exit()
    

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: