Hi all my euler 29 is off by 200 where I get 9048 rather than the 9183
I attempted to find the ans search for it in the list (I used a binary search since linear was taking very long) and would just recieve the value 9048 somehow, I don’t know if there is something wrong with the searching aspect and many distinct powers are getting discarded as repeats by code by accident.
Here is my code
def insertionSort(list):
for i in range(1, len(list)):
key = list[i]
j = i-1
while j >= 0 and key < list[j] :
list[j + 1] = list[j]
j -= 1
list[j+1] = key
def binary_search(list, ans):
insertionSort(list)
left = 0
right = len(list) - 1
while left <= right:
mid = (left + right) // 2
if list[mid] == ans:
return mid
elif list[mid] < ans:
left = mid + 1
else:
right = mid - 1
return -1
list=[]
for a in range(2,100):
for b in range(2,100):
yes=0
ans=a**b
result = binary_search(list,ans)
if result==-1:
list.append(ans)
print(len(list))
Looking for any help, thanks.
New contributor
Dill is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.