zoukankan      html  css  js  c++  java
  • Project Euler Problem 7: 10001st prime

    10001st prime

    Problem 7

    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

    What is the 10 001st prime number?


    C++:

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    const int MAXN = 10001;
    
    int prime[MAXN+1] = {2, 3};
    
    bool isprime(int n)
    {
        int q=sqrt(n), i=1;
    
        while(prime[i] <= q)
            if(n % prime[i] == 0)
                return false;
            else
                i++;
    
        return true;
    }
    
    void maketable(int n)
    {
        int i = 2, val = 5;
    
        while(i < n) {
            if(isprime(val))
                prime[i++] = val;
    
            val += 2;
        }
    }
    
    int main()
    {
        int n;
    
        maketable(MAXN);
    
        while(cin >> n)
            cout << prime[n - 1] << endl;
    
        return 0;
    }


    C++:

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    const int MAXN = 10001;
    
    int prime[MAXN+2] = {2, 3, 5};
    
    bool isprime(int n)
    {
        int q=sqrt(n), i=1;
    
        while(prime[i] <= q)
            if(n % prime[i] == 0)
                return false;
            else
                i++;
    
        return true;
    }
    
    void maketable(int n)
    {
        int val1 = 1, val5 = 5;
    
        for(int i=3; i<=n;) {
            val1 += 6;
            if(isprime(val1))
                prime[i++] = val1;
    
            val5 += 6;
            if(isprime(val5))
                prime[i++] = val5;
        }
    }
    
    int main()
    {
        int n;
    
        maketable(MAXN);
    
        while(cin >> n)
            cout << prime[n - 1] << endl;
    
        return 0;
    }



  • 相关阅读:
    MySQL ——索引原理与慢查询优化(Day45)
    mysql 练习题(Day44)
    MySQL 多表查询(Day43)
    MySQL 单表查询(Day42)
    MySQL -表完整性约束(Day41)
    回调函数
    进程池
    共享数据, 信号量(了解),事件(了解)
    管道
    python并发编程之多进程
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564036.html
Copyright © 2011-2022 走看看