2023-06-06 19:42:51 +02:00

52 lines
1.2 KiB
Python

#!/usr/bin/env python3
# Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
#
# 2^2=4, 2^3=8, 2^4=16, 2^5=32
# 3^2=9, 3^3=27, 3^4=81, 3^5=243
# 4^2=16, 4^3=64, 4^4=256, 4^5=1024
# 5^2=25, 5^3=125, 5^4=625, 5^5=3125
#
# If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
#
# 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
#
# How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
from timeit import default_timer
from numpy import zeros
def main():
start = default_timer()
powers = zeros(9801)
# Generate all the powers
for i in range(2, 101):
a = i
for j in range(2, 101):
powers[(i-2)*99+j-2] = a ** j
# Sort the values and count the different values.
powers = list(powers)
powers.sort()
count = 1
for i in range(1, 9801):
if powers[i] != powers[i-1]:
count = count + 1
end = default_timer()
print('Project Euler, Problem 29')
print(f'Answer: {count}')
print(f'Elapsed time: {end - start:.9f} seconds')
if __name__ == '__main__':
main()