Hey, i am having my first course in c++, it was all going very easy until i had my mid-term yesterday the first question in the exam sheet was to write a program to generate and print out the following series :
6 8 11 15 16 21 22 26 29 31
i solved it in too many lines using a lot of loops, but the lecturer told me it could be solved using one "for" loop containing one "if" statement however he didn't tell me how he just said think about it :S but i am too curious i need to know now :D
Okay, I figured it out. It's not too hard. I'll tell you the next three numbers: 35 37 40.
To solve this you just need to look at the differences between the elements in the series.
i tried but i just can't get. the first four can be generated by a loop and the last four also can generated but 16, 21 seems out of the series order and its supposed to be solved using one "for" loop containing one "if" statement. can you put in code so i could understand it.
The difference of even number POSITION in the series is 7 6 5 5 and that of odd numbers POSITION is 5 5 6 7
Just observed a pattern.
Now considering the difference which is there in the successive numbers
i.e. 2 3 4 1 5 1 4 3 2
This is the number of times the Greatest Prime Factor of a number is the Greatest Prime Factor of numbers less than or equal to the number. Phew!! that was too much.
If this is it then ur instructor has some sort of grudge on all of you :)
GPF of a number n is defined as the largest prime number that divides n. Assuming for 1 it is 1 we have for numbers till 23 the values as:
1 1
2 2
3 3
4 2
5 5
6 3
7 7
8 2
9 3
10 5
11 11
12 3
13 13
14 7
15 5
16 2
17 17
18 3
19 19
20 5
21 7
22 11
23 23
Now ur series corresponds to values from 14 to 23. Take 14. Its GPF is 7. How many times u can see 7 as the GPF of numbers less than or equal to14? '2' times. U just got the first number in the difference. Take 15. Its GPF is 5. How many times u can see 5 as the GPF of numbers less than or equal to 15? 3 times.U just got the second number in the difference. Similarly for 16. For 17 there is just one number which is less than or equal to 17 having 17 as GPF (which is 17 itself). So for prime number u can directly conclude the value as 1. So now u have:
14 2
15 3
16 4
17 1
18 5
19 1
20 4
21 3
22 2
23 1
Now for implementation u can use the following equation:
For n>1: a(n)=1 iff n is prime;
a(n) = n/p for n<=p*(p+1) and p = greatest prime factor of n.
If both these conditions are not satisfied then u will have to do it by counting. For example take 16. Its GPF is 2. But p*(p+1) = 6 which is less than 16.