Answer to Question #283704 in Python for alk

Question #283704

Write a program to print the LARGEST ODD fibonaci number in a given range. If the number doesn't fall in the given range then print 0.



Example 1:

Given input

100

250

Expected output

233



Example 2:

Given input

4000

11000

Expected output

6765


1
Expert's answer
2021-12-30T07:13:52-0500
# Python 3.9.5

def is_perfect_square(n):
    x = n
    y = (x + 1) // 2
    while y < x:
        x = y
        y = (x + n // x) // 2
    if x**2 == n:
        return True
    else:
        return False

def enter_range():
    fib_range = input('Enter range separate by space: ').split()
    fib_range = [int(i) for i in fib_range]
    return fib_range

def get_fib_number(fib_range):
    result = 0
    for i in range(fib_range[0], fib_range[1]+1):
        if is_perfect_square(5 * i * i + 4) or is_perfect_square(5 * i * i -4):
            if i % 2 == 1:
                result = i
    return result

def main():
    fib_range = enter_range()
    print(get_fib_number(fib_range))

if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS