V
V
vadimkenny2020-11-12 20:35:54
Python
vadimkenny, 2020-11-12 20:35:54

How to solve a problem by recursive algorithm in python?

I can not solve the problem, you need to do it in python. The answer should be 164, I attached my wrong code and task.5fad6f27a71b8957957359.png

k=0
c=0
def f(n):
    if n <=5:
        return n + 15
    if n % 2 == 0 and n > 5:
        return f(n//2)+n*n*n-1
    if n % 2 != 0 and n>5:
        return f(n-1) + 2*n*n+1

for i in range(1,1001):
    x = f(i)
    while x>0:
        a = x % 10
        if a == 8:
            k = k + 1
        x = x // 10
    if k>=2:
        c = c + 1
        
print(c)
input()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-11-12
@vadimkenny

You seem to have forgotten to reset the count of eights (k) for each value of f.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question