Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python/Java]finding prime num in n range template #8

Open
ZhangCheng-zh opened this issue Jul 30, 2022 · 0 comments
Open

[Python/Java]finding prime num in n range template #8

ZhangCheng-zh opened this issue Jul 30, 2022 · 0 comments
Labels

Comments

@ZhangCheng-zh
Copy link
Owner

ZhangCheng-zh commented Jul 30, 2022

erichsen

MX = 10 ** 6 + 1
primeList = []
is_prime = [True] * MX
for i in range(2, MX):
    if is_prime[i]:
        primeList.append(i)
        for j in range(i * i, MX, i):
            is_prime[j] = False
primeList.extend((MX, MX))  # 保证下面下标不会越界
function erichsen (n) {
    const isPrimeList = new Array(n + 1).fill(1)
      for (i = 2; i <= n / i; i++) {
          if (isPrimeList[i] === 1) {
              // delete i Multiple number
              for (let j = i * i; j <= n; j += i) {
                  isPrimeList[j] = 0
              }
          }
      }
  
      let count = 0
      for (let i = 2; i <= n; i++) {
          if (isPrimeList[i] === 1) count++
      }
      return count
  }

euler

class Solution {
        private final static int MX = 1000000;
    private final static ArrayList<Integer> primes = new ArrayList<Integer>();

    static {
        boolean[] noPrime = new boolean[MX + 1];
        for (int i = 2; i <= MX; i++) {
            if (!noPrime[i]) {
                primes.add(i);
            }

            for (int p: primes) {
                if (i * p > MX) break;
                noPrime[i * p] = true;
                if (i % p == 0) {
                    break;
                }
            }
        }
        // optional suffix, to avoid overflow
        primes.add(MX + 1);
        primes.add(MX + 1);
    }

    // todo
}
function euler(n) {
 const isPrimeList = new Array(n + 1).fill(1)
 const primes = []
 
 let k = 0
 let count = 0
 for (let i = 2; i <= n; i++) {
  if(isPrimeList[i] === 1) { primes[k++] = i , count++ }
  for (let j = 0; primes[j] * i <= n; j++) {
     // 每个质数都和i相乘 得到合数
     isPrimeList[primes[j] * i] =  0
    // 在删除的过程中,只通过数的最小质因数筛数。
     if ( i % primes[j] === 0) break
  }
 }
}
@ZhangCheng-zh ZhangCheng-zh changed the title The methods of finding prime num in n range [Python/Java]finding prime num in n range template Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant