Blame view

NumberTheoryandCombinatorics/SieveofEratosthenes/SieveOfEratosthenes.cpp 830 Bytes
743b077f4   Ronaldo   Update.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  //
  // Created by ronal on 2/11/2023.
  // Problem Description Link.
  // https://practice.geeksforgeeks.org/problems/sieve-of-eratosthenes5242/1
  
  //User function Template for C++
  class Solution
  {
  public:
      vector<int> sieveOfEratosthenes(int N)
      {
          vector<int> primes;
          for(int i = 0; i <= N; i++){
              if(isPrime(i)){
                  primes.push_back(i);
              }
          }
          return primes;
      }
  
      bool isPrime(unsigned long long n){
  
          if(n == 2 || n == 3){
              return true;
  
          }else if(n <= 1 || n % 2 == 0 || n % 3 == 0){
              return false;
          }else{
              for(int i = 5; i * i <= n; i += 6){
                  if(n % i == 0 || n % (i + 2) == 0){
                      return false;
                  }
              }
          }
          return true;
      }
  };