There is a assigment I am doing for Beginners in AI and Python.
Create a Class NewInt that inherits from int. It should have an Instance Method is_fibonacci () that returns True if the number is a Fibonacci number, False if not. Generate a list with NewInt from 0 to 1000. Then create a List Comprehension that only retains the numbers that are Fibonacci using the class and instance method you created.
[0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] #Expected output for task
I made this code for the task
import math
class NewInt(int):
def is_Perfect_Square(x):
s = int(math.sqrt(x))
return s*s == x
def is_fibonacci(n):
return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
print(fibonacci_List)
It worked few hours ago, but i get errors like this:
TypeError Traceback (most recent call last)
<ipython-input-8-377e0f9b7814> in <module>
1 import math
2
----> 3 class NewInt(int):
4
5 def is_Perfect_Square(x):
<ipython-input-8-377e0f9b7814> in NewInt()
11 return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
12
---> 13 fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
14 print(fibonacci_List)
<ipython-input-8-377e0f9b7814> in <listcomp>(.0)
11 return is_Perfect_Square(5*n*n + 4) or is_Perfect_Square(5*n*n - 4)
12
---> 13 fibonacci_List = [i for i in range(0,1000)if NewInt().is_fibonacci(i)]
14 print(fibonacci_List)
TypeError: is_fibonacci() takes 1 positional argument but 2 were given
Can someone help me point out my mistakes? I am new to Python.
NewInt.is_fibonacci(i), but the wholeNewIntclass does not make all that much sense. Also, why not just generate the fibonacci sequence instead of testing each number whether it is a fibonacci number?selfto contain the instance reference.