The lucky numbers were named by the Polish mathematician Ulam. It is generated using a "sieve" similar to the generation of prime numbers
First, write the natural numbers 1, 2, 3, 4, 5, 6 from 1,....
1 is the first lucky number.
Let's start with the number 2. Delete all items whose ordinal numbers are divisible by 2 and become:
1 _ 3 _ 5 _ 7 _ 9 ....
Tighten them down and reorder them as:
1 3 5 7 9 ....
In this case, 3 is the second lucky number, and then all numbers divisible by 3 are removed. Note that it is the ordinal position, not whether the number itself can be divisible by 3!! The ones deleted should be
5,11, 17, ...
In this case, 7 is the third lucky number, and then delete the ordinal position that is divisible by 7 (19, 39,...)
The final remaining sequence looks like this:
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, ...
Input format
Enter two positive integers m n, separated by spaces (m < n < 1000*1000)
Output format
Program output The number of lucky numbers between m and n (excluding m and n).
Sample input
30 69
Sample output
8
fun:
void fun_lucky(int step) {
if (a[step] > n) return 0;//最大不能超过n
int cnt = step;
for (int i = step; i < n; i++) {
if (i % a[step])
a[cnt++] = a[i];//从当前位置往后遍历
// 如果i不能被当前位置的数整除 则更新数组中的元素
}
fun_lucky(step + 1);//递归,新数组的当前位置遍历
}
main:
int main() {
int res = 0;
::scanf("%d%d",&m,&n);
for (int i = 1; i <= n; i++)
a[i] = i * 2 - 1;//给奇数
fun_lucky(2);//从2开始
for (int i = 1; a[i] < n; i++)
if (a[i] > m) res++;
::printf("%d", res);
}