Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 8th, 2012, 04:56 PM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation 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
November 8th, 2012, 05:32 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,214
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 10 m 8 sec
Reputation Power: 455
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.
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!

#3
November 9th, 2012, 10:04 AM
 Nightmareix35
Contributing User

Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 2
Quote:
 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.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python problem; big number handling!