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
28
# from code by Oma Falk
"""
https://www.sololearn.com/Discuss/3289470/?ref=app
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
"""
# This can solve for n as big as Python can handle in your device, in the fastest way, too.
# separate the arr computation for improved efficiency. This will just produce constant parameters anyway, so it does not have to be recomputed everytime.
def precalc(func):
# precomputing values for arr, offset and period
arr = [1,1] #base case f(0)==f(1)==1
prs = [(1,1)]
offset, period, i = 0, 1, 0
while True:
i+=1
nxval = func(arr[-1],arr[-2])
arr.append(nxval)
nxpr = arr[-2],arr[-1]
if nxpr in prs:
idx = prs.index(nxpr)
offset = idx
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run