Problem Statement
Vasa likes to construct sequences of numbers. If you tell him a positive integer n, he will begin a new sequence by writing the integer n as its first element. He will then repeat the following steps:
- If the last number in his sequence is a prime, he terminates the sequence.
- If he already wrote n elements without finding a prime, he becomes bored and leaves.
- Otherwise, he computes the next element of the sequence as the sum of squares of digits of the last element. For example, 4001 will be followed by 4^2 + 0^2 + 0^2 + 1^2 = 17, and 17 will be followed by 1^2 + 7^2 = 50.
Find out what happens for the given
Definition
- Class:
- ExploringNumbers
- Method:
- numberOfSteps
- Parameters:
- int
- Returns:
- int
- Method signature:
- int numberOfSteps(int n)
- (be sure your method is public)
Notes
- A prime number is a positive integer with exactly two positive integer divisors: 1 and itself. The first few primes are 2, 3, 5, 7, 11. Note that 1 is not a prime.
Constraints
- n will be between 1 and 10^9, inclusive.
Examples
5
Returns: 1
The input itself is a prime.
57
Returns: 4
Vasa will write down 57, 74 (= 5^2 + 7^2), 65 (= 7^2 + 4^2), and 61 (= 6^2 + 5^2). Number 61 is a prime.
1
Returns: -1
Vasa begins by writing down the number 1. As 1 is not a prime, he is not done yet. As he already wrote down 1 element of the sequence without finding a prime, he becomes bored and leaves.
6498501
Returns: 2
989113
Returns: 6
12366
Returns: -1
For n=12366 there are no primes among the first 12366 elements of Vasa's sequence.
999999999
Returns: 7
1000000000
Returns: -1
900000011
Returns: 1
19112
Returns: 10
999999991
Returns: 4
999999997
Returns: 4
25
Returns: 2
34
Returns: 3
2
Returns: 1
3
Returns: 1
4
Returns: 3
5
Returns: 1
6
Returns: 4
7
Returns: 1
8
Returns: 4
9
Returns: 4
1111111
Returns: 2
998812807
Returns: 6
997927811
Returns: 5
998054383
Returns: 2
999999987
Returns: -1
17
Returns: 1
999884959
Returns: 1
982451653
Returns: 1
49
Returns: 2
40009
Returns: 1
999999937
Returns: 1
111121380
Returns: -1
623610000
Returns: -1
999000016
Returns: -1
941110000
Returns: -1
100000000
Returns: -1
77
Returns: 8
1162
Returns: 6
900000008
Returns: 7
999992573
Returns: 1
952
Returns: 3
999981110
Returns: -1
999987511
Returns: -1