#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    32
    Rep Power
    2

    Python problem; big number handling!


    This is the given problem.
    Write a function in Python that receives as input 5^100000. the program must calculate and return the sum of the 60000 maximal consecutive digits. Sample: If input is 123456789, the output must be 35 (5+6+7+8+9).

    Note: the program must be as efficient as possible. Run time: a few seconds to one minute maximum on a lower end computer.

    The program must output 269304 for checking purposes.

    Help anyone?
    I tried a few ways around but it always takes my computer from 2 to 3 minutes to complete the calculation...
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Few seconds is way too long for this calculation.

    suppose you have a number with digits (and these are integers 0 through 9)
    a b c d e f
    you need to find the maximal sum of 3 consecutive digits.
    so you start with the sum
    a+b+c
    You next need
    b+c+d
    do you have to add b, c, and d?
    No! You can subtract `a' and add `d'. And that's a saving for sequences longer than 3.

    Oh dear too many clues.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    32
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Few seconds is way too long for this calculation.

    suppose you have a number with digits (and these are integers 0 through 9)
    a b c d e f
    you need to find the maximal sum of 3 consecutive digits.
    so you start with the sum
    a+b+c
    You next need
    b+c+d
    do you have to add b, c, and d?
    No! You can subtract `a' and add `d'. And that's a saving for sequences longer than 3.

    Oh dear too many clues.

    Oh...My...God!

    HAHAHA! what was I thinking I really thought this would turn out to be way harder, I guess the time limitation thing brought me to think way too far methods.

    Dear sir, you saved me from "me". Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo