0

I have this school project, but I ran into an issue that my code give me an infinite int when I go over the length of 1200

n = int(input())
s, counting = 4, 0
s_lst = []
if n == 0:   print(0)
elif n == 1: print(4)
else:
    for i in range(1, n):
        if i % 2 == 1:
            s = s * 1.5
        else: 
            s = s * 2
    print(int(s%(10**9+7)))

How do I prevent the int from becoming infinite

Solved the issue by removing the float multiplication

n = int(input())
s, counting = 4, 0
s_lst = []
mult = 1
if n == 0:   print(0)
elif n == 1: print(4)
else:
    for i in range(1, n):
        if i % 2 == 1:
            s_lst.append(1.5)
        else: 
            s = int(s * 2)
    if len(s_lst) % 2 == 0:
        for i in range(len(s_lst)//2):
            mult *= 3
    else:
        for i in range(len(s_lst)//2+1):
            mult *= 3
        mult = mult // 2
    
    print((s * mult)%(10**9+7))
kotm8
  • 1
  • 1
  • What `int` is infinite? (I don't see any infinite loops here.) – chepner Oct 08 '22 at 21:32
  • 2
    You are trying to compute, effectively, `4 * (1.5 ** 600) * (2 ** 600)`. That is going to be a `float` because of the `1.5`, but the result is too large for the range of a `float` value so it overflows to infinity. – kaya3 Oct 08 '22 at 21:34
  • The answer 's' becomes infinite when I set the input over 1200 – kotm8 Oct 08 '22 at 21:34
  • What makes you think `s` is an `int`? – chepner Oct 08 '22 at 21:36
  • `s` exceeds Python's max float value `1.7976931348623157e+308` when `n >= 1901 at `n = 1900 s = 1.1072336853488967e+308` and the output is `42185108`, – Yuri Ginsburg Oct 08 '22 at 21:45
  • @kaya3 Thanks for the tip i was able to transfor the 1.5 float multiplication into a intiger mutiplication and division and now it works. – kotm8 Oct 08 '22 at 21:52

0 Answers0