PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''
my question is listed below please find answer
Problem Statement: Alex is exploring a series and she came across a special series, in which f(N)=f(N-1)*(N-1)+(N-2)*(N-2) mod 47 where f(0) = 1, f(1) = 1 Your task is to help Alex find and return an integer value, representing the Nth number in this special series Note: Return the output modulo 47. Input Specification: input1: An integer value N. Output Specification: Return an integer value, representing the Nth number in this special series. Sample Input: 4 Sample Output: 29
'''
from functools import cache
def f(i):
#internal function
@cache
def f2(i):
if i==0 or i==1:
return 1
return (f2(i-1)*f2(i-1)+f2(i-2)*f2(i-2))%47
# this is surprising...
# range looping needed to build cache!
# @cache would not work otherwise if we just use f2 directly.
res = 1
for j in range(2,i+1):
res = f2(j)
return res
print("f(1000000) =", f(1000000))
for i in range(0, 51):
print(f"f({i}) = {f(i)}")
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run