Problem of the Day
A new programming or logic puzzle every Mon-Fri

Six Times

There exists at least one number that is 6 times the sum of its digits. What is the first occurrence of this phenomenon and are there any other numbers with this same pattern? For bonus points write a program to solve this problem.

Permalink: http://problemotd.com/problem/six-times/

Comments:

  • parth - 10 years, 3 months ago

    54

    only one number.

    js:

    for (var i=10;i<10000000;i++){ if ( sumOfDigits(i)*6 == i ){ console.log(i); } }

    function sumOfDigits(d){ d = parseInt(d); var sum = 0; while (d > 0){ sum += d%10; d = parseInt(d/10); } return sum; }

    reply permalink

  • Anonymous - 10 years, 3 months ago

    0 is the first, 54 is the second

    def sum_digits(n):
        return sum(map(int, str(n)))
    
    for i in range(1000000):
        if 6*sum_digits(i) == i:
            print(i)
    

    reply permalink

  • Anonymous - 10 years, 3 months ago

    There are two such numbers : 0 and 54

    Negative and non-integer numbers are ruled out by the fact that the sum of the digits of a number is always a positive integer.

    So we are left with positive integers, for which we should solve the following indetermined sum (a[i] is the i-th digit of a (starting at i=0), and n is the number of digits of a):

    0 == \sum_{i = 0}^{n-1} (6 - 10i) * a[i]

    Note that for i=0, (6 - 10i) = 5, which is the only positive term.

    The next one is -4.

    Then, we are left with numbers < -90. Since 0 <= a[i] <= 9, those cannot be compensated by the first (positive) term (since 9*5 = 45).

    So the only possibilities are 0 and 54.

    reply permalink

  • wobblebucket - 10 years, 3 months ago

    x = 1
    while x != (sum(map(int, str(x))) * 6):
        x = x + 1
    print x
    

    reply permalink

  • btgrant-76 - 10 years, 3 months ago

    A little Scala. No state mutations and par produces a parallel collection.

      println((1 to 1000000).par
        .map { num: Int =>
          (num, num.toString.map {c: Char => c.asDigit }.sum)
        }.filter { numAndSum: (Int, Int) =>
          numAndSum._1 == numAndSum._2 * 6
        }.map { numAndSumTimesSix: (Int, Int) =>
          numAndSumTimesSix._1
        }.mkString(", "))
    

    reply permalink

  • asheehan - 10 years, 2 months ago

    thought I submitted one for this, but don't see it here. Hmmm... Here's my program in Ruby:

    
    upperBound = ARGV[0].to_i
    for i in 0..upperBound
        digits = i.to_s.chars.map(&:to_i)
        sum = 0
        digits.each do |digit|
            sum += digit
        end
    
        if i == (sum * 6)
            puts i
        end
    end
    
    # output: 0, 54
    

    reply permalink

Content curated by @MaxBurstein