Wednesday, February 3, 2021

Recursion in Python. Having fun writing code to help out unhappy kids

The only purpose of python coding below is to prevent kids from straight forward calculations, which actually are boring rather then difficult. In doing so I intend to teach my students pretty old principle "Just think first"

"Problem" itself :- 

Algorithm for calculating the value of the function F (n), where n is a natural number,

is given by the following relations:

F (n) = 1 for n = 1;

F (n) = n + F (n - 1), if n is even,

F (n) = 2 × F (n - 2) if n> 1 and n is odd.

What is the value of the function F (46)?


Just notice that number 46 could be easily replaced by 56 or 76






















Another samples :-

The algorithm for calculating the function F (n) is given by the following relations:
F (n) = n for n≤3;
F (n) = n ∗ n ∗ n + F (n – 1) if n > 3 and gives remainder 0 when divided by 3
F (n) = 4 + F (n // 3) if n > 3 and gives remainder 1 when divided by 3
F (n) = n ∗ n + F (n – 2) if n  > 3 and gives remainder 2 when divided by 3
Here // stands for integer division. 
Output an answer as binary value of F(100).



Algorithm for calculating functions F (n), where n is a natural number, given;by the following ratios:

F (n) = n - 1 for n < 4 ,

F (n) = n + 2 * F (n - 1) when n  > 3 and a multiple of 3 ,

F (n) = F (n - 2) + F (n - 3) when n > 3 and not a multiple of 3 .

What is the sum of the digits of the value of the function F (65)?



The algorithm for calculating the function F (n) is given by the following relations:

F (n) = n for n≤3;
F (n) = n ∗ n ∗ n + F (n – 1) if n > 3 and gives remainder 0 when divided by 3
F (n) = 4 + F (n // 3) if n > 3 and gives remainder 1 when divided by 3
F (n) = n ∗ n + F (n – 2) if n  > 3 and gives remainder 2 when divided by 3
Here // stands for integer division.
Determine the number of natural values n from the segment [1; 1000], for which the sum of digits the F (n) value is 53.
































No comments:

Post a Comment