Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re (tilly) 1: Finding sum of all digits from 1 to 1 million.

by PrakashK (Pilgrim)
on Jan 24, 2002 at 00:59 UTC ( #141024=note: print w/ replies, xml ) Need Help??


in reply to Re (tilly) 1: Finding sum of all digits from 1 to 1 million.
in thread Finding sum of all digits from 1 to 1 million.

a good exercise would be to write Perl code that efficiently calculates the sum of all of the digits in a range of integers.
Assuming sum(n) is the sum of all digits from 1 to n, sum_range(m, n) is the sum of all digits from m to n, and m < n, isn't the following true?
sum_range(m,n) = sum(n) - sum(m)
Or, did I totally get you wrong? /prakash


Comment on Re: Re (tilly) 1: Finding sum of all digits from 1 to 1 million.
Select or Download Code
Re (tilly) 3: Finding sum of all digits from 1 to 1 million.
by tilly (Archbishop) on Jan 24, 2002 at 01:06 UTC
    Off by one error again, your formula drops m from the answer. (And the obvious fix will need more cases if your range potentially includes negative integers.)

    But still, you are essentially correct. After some wrapper logic, the problem really does boil down to being able to efficiently calculate the sum of the digits from 1 to n for any positive integer n.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://141024]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-12-20 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls