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

Minify Sum

Happy Monday!

Today's problem involves turning a long number in to a short number. The goal is to sum each digit of the number recursively until you reach a single digit. Here are a few test cases to make it easier to understand.

123456 = 1+2+3+4+5+6 = 2+1 = 3

99 = 9+9 = 1+8 = 9

Permalink: http://problemotd.com/problem/minify-sum/

Comments:

  • Sami Suteria - 9 years, 6 months ago

    Run with node with the argument being after the filename $: node app.js 123456

    var input = 999;
    
    if(process.argv[2]){
        input = process.argv[2];
    }
    
    console.log(minify(input));
    
    function minify (input)
    {
        var sum = 0;
        input.toString().split("").forEach(function (value){
            sum += parseInt(value);
        });
        if (sum > 9){
            return minify(sum);
        }
        return sum;
    }
    

    reply permalink

  • Anonymous - 9 years, 6 months ago

    #python
    def minify(number):
        minifiedsum = 0
        while number > 0:
    
            minifiedsum = minifiedsum + number % 10
            number = number/10
    
        if minifiedsum > 9:
            return minify(minifiedsum)
        else:
            return minifiedsum
    

    reply permalink

  • Anonymous - 9 years, 6 months ago

    def minify_sum(n):
        return minify_sum(sum(map(int, str(n)))) if n > 9 else n
    

    reply permalink

Content curated by @MaxBurstein