Created
December 31, 2011 23:38
-
-
Save jakedobkin/1545655 to your computer and use it in GitHub Desktop.
Euler 92
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://projecteuler.net/problem=92 | |
# this version takes advantage of the fact that all numbers up to 10MM have roundfadds < 600 | |
# so first compute those, then check them for each number from 1 to 10MM | |
# that shaves it from 6 minutes to 120s | |
def fadd(n): | |
sum = 0 | |
for i in range (0,len(str(n))): | |
sum += int(str(n)[i:i+1])**2 | |
return sum | |
def roundfadd(n): | |
if array[n] != 0: | |
return array[n] | |
if fadd(n) == 1: | |
return 1 | |
elif fadd(n) == 89: | |
return 89 | |
else: | |
return roundfadd(fadd(n)) | |
array = [0]*600 | |
for x in range (1,600): | |
array[x]=roundfadd(x) | |
top = 10000000 | |
count = 0 | |
for r in range (1,top): | |
if array[fadd(r)] == 89: | |
count += 1 | |
print count |
Author
jakedobkin
commented
Jan 2, 2012
via email
i guess numbers work faster than strings?
jake
Jake Dobkin
Gothamist.com
(p) 917 627 6915
(f) 646 349 3893
AIM, YIM, GTalk: jakedobkin
Mediakit: www.gothamistllc.com
…On Sun, Jan 1, 2012 at 9:20 PM, David Jacobs < ***@***.*** > wrote:
changing fadd to the above cut about processing time from ~160s to 40s on
my macbook air. There's also a similar solution here:
http://rosettacode.org/wiki/Happy_Number#Python
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1545655
Yes - I think stringification is expensive.
On Sun, Jan 1, 2012 at 9:36 PM, jakedobkin
[email protected]
wrote:
i guess numbers work faster than strings?
jake
Jake Dobkin
Gothamist.com
(p) 917 627 6915
(f) 646 349 3893
AIM, YIM, GTalk: jakedobkin
Mediakit: www.gothamistllc.com
On Sun, Jan 1, 2012 at 9:20 PM, David Jacobs <
***@***.***
> wrote:
>
> changing fadd to the above cut about processing time from ~160s to 40s on
> my macbook air. There's also a similar solution here:
> http://rosettacode.org/wiki/Happy_Number#Python
> ---
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/1545655
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1545655
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment