We define:
Find the sum of all integers between 1 and 1000 inclusive such that ϕ ( n ) = ϕ ( Ψ ( F ( n ) ) ) .
Note : F 0 = 0 , F 1 = 1 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
What will be the result for n between 1 and 2000?
Log in to reply
Log in to reply
I think it's Fibonacci function. Even if its complexity is in l o g ( n ) . It is called n times. So we end up with something near n l o g ( n ) .
Log in to reply
@Abdelhamid Saadi – yes but it's better than using the original recursive function, jumping from 2k to k saves a lot of operations.
@Abdelhamid Saadi
–
The thing what is mainly slowing the code is the
fib(n)
function try using the iterative fibonacci like...
1 2 3 4 5 |
|
The recursive algorithm you are using is recomputing values again and again which makes it inefficient, so you can store the already computed values of
fib(k)
and access it instead of recomputing.
Here is my solution in Python:
def f(n):
s=0 m=n while m>0: s+=m%10 m/=10 return s
fib=[0,1]
for i in range(2,1001):
fib.append(fib[i-1]+fib[i-2])
facto=[1]
for i in range(1,9001):
facto.append(facto[i-1]*i)
c=0
for i in range(1,1001):
if f(facto[i])==f(facto[f(fib[i])]): c+=i
print c
there is a solution in python 3.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Problem Loading...
Note Loading...
Set Loading...
Here's my solution in python: