luckyのnum.cpp

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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值