Skoči na vsebino

P1 - Nedvoumna stevila

from time import time
limit = int(input("Define the upper limit (100 000): ") or 100000) + 1

timeRun = time()
ulamNums = [1, 2]

for current in range(3, limit):
  found = 0
  indexTop = len(ulamNums) - 1
  indexBot = 0

  while indexTop > indexBot:
    val = ulamNums[indexTop] + ulamNums[indexBot]

    if val < current:
      indexBot += 1
    elif val > current:
      indexTop -= 1
    else:
      found += 1
      if (found > 1):
        break
      indexTop -= 1
      indexBot += 1


  if found == 1:
    ulamNums.append(current)



timeRun = time() - timeRun
print("First and last 50 numbers;")
print(ulamNums[:25])
print(ulamNums[-25:])
print("Finished in %s sec" % timeRun)

Zadnja posodobitev: February 27, 2022