Array ( [0] => [1] => questions [2] => Codevita-Previous-Questions [3] => Game-Of-Primes ) Codevita Previous Questions | Game Of Primes | THE INQUISITIVE





Game Of Primes

LEVEL:Beginner

Description

n a global Mathematics contest, the contestants are told to invent some special numbers which can be built by adding the squares of its digits. Doing this perpetually, the numbers will end up to 1 or 4.
If a positive integer ends with 1, then it is called the Number of Game.
An example from above is:
13 = 1^2 + 3^2 = 1+9 = 10 (Step:1)
10 = 1^2 + 0^2 = 1+0 = 1 (Step:2), iteration ends in Step 2 since number ends with 1
Then in next round, the contestants are asked to combine their newly invented number, i.e. Number of Game with prime number.
Now, being a smart programmer, write a program to help the contestants to find out the Nth combined number within any given range, where N can be any integer.

Input Format

Input consists of 3 integers X, Y, N, one on each line. X and Y are upper and lower limits of the range. The range is inclusive of both X and Y. Find Nth number in range [X,Y].
Constraints:
X <= Y
X > 0
N > 0

Output Format

Print the resultant value


Example 1:

Input
1
30
3
Output
19
Example 2:

Input
40
100
2
Output
97
Example 3:

Input
20
100
3
Output
79

oops

Login to see Discussion




Approach

Step-1) take input l as lower value u as upper value and m as condition and initialize count as 0.
Step-2) now rotate a loop from l to u+1
Step-3) check whether the number is happy number or not.
Note: A number is called happy if it leads to 1 after a sequence of steps wherein each step number is replaced by the sum of squares of its digit that is if we start with Happy Number and keep replacing it with digits square sum, we reach 1.
Step-4) If a number is happy number check the number is prime number or not
Step-5) If a number is both happy number and prime number then increment the count value by 1
Step-6) Check if the given condition m is equal to m then print that number and break for loop.


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution