-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectEuler63.py
37 lines (33 loc) · 1.01 KB
/
ProjectEuler63.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import cProfile
import io
import pstats
#Project Euler Problem 63
#
# The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.
#
# How many n-digit positive integers exist which are also an nth power?
# print(len([x for x in range(1,10000000001) for y in range(1,100001) if y**len(str(x))==x]))
def profile(fnc):
"""A decorator that uses cProfile to profile a function"""
def inner(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = fnc(*args, **kwargs)
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
return retval
return inner
@profile
def main():
c= 0
for x in range(1,10001):
for y in range(1,10):
a = str(y**x)
if len(a) == x:
c+=1
print(c, 'integers are nth-powers')
main()