给定一个数字N,任务是打印所有小于或等于N的素数。 例如:
null
Input: 7Output: 2, 3, 5, 7Input: 13Output: 2, 3, 5, 7, 11, 13
天真的方法: 从2迭代到N,并检查 首要的 .如果是素数,请打印数字。 以下是上述方法的实施情况:
C++
// C++ program to print all primes // less than N #include <bits/stdc++.h> using namespace std; // function check whether a number // is prime or not bool isPrime( int n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for ( int i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function to print primes void printPrime( int n) { for ( int i = 2; i <= n; i++) { if (isPrime(i)) cout << i << " " ; } } // Driver Code int main() { int n = 7; printPrime(n); } |
Python3
# Python3 program to print # all primes less than N # Function to check whether # a number is prime or not . def isPrime(n): # Corner case if n < = 1 : return False # check from 2 to n-1 for i in range ( 2 , n): if n % i = = 0 : return False return True # Function to print primes def printPrime(n): for i in range ( 2 , n + 1 ): if isPrime(i): print (i, end = " " ) # Driver code if __name__ = = "__main__" : n = 7 # function calling printPrime(n) # This code is contributed # by Ankit Rai |
JAVA
// Java program to print // all primes less than N class GFG { // function check whether // a number is prime or not static boolean isPrime( int n) { // Corner case if (n <= 1 ) return false ; // Check from 2 to n-1 for ( int i = 2 ; i < n; i++) if (n % i == 0 ) return false ; return true ; } // Function to print primes static void printPrime( int n) { for ( int i = 2 ; i <= n; i++) { if (isPrime(i)) System.out.print(i + " " ); } } // Driver Code public static void main(String[] args) { int n = 7 ; printPrime(n); } } // This code is contributed // by ChitraNayal |
C#
// C# program to print // all primes less than N using System; class GFG { // function check whether // a number is prime or not static bool isPrime( int n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for ( int i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function to print primes static void printPrime( int n) { for ( int i = 2; i <= n; i++) { if (isPrime(i)) Console.Write(i + " " ); } } // Driver Code public static void Main() { int n = 7; printPrime(n); } } // This code is contributed // by ChitraNayal |
PHP
<?php // PHP program to print // all primes less than N // function check whether // a number is prime or not function isPrime( $n ) { // Corner case if ( $n <= 1) return false; // Check from 2 to n-1 for ( $i = 2; $i < $n ; $i ++) if ( $n % $i == 0) return false; return true; } // Function to print primes function printPrime( $n ) { for ( $i = 2; $i <= $n ; $i ++) { if (isPrime( $i )) echo $i . " " ; } } // Driver Code $n = 7; printPrime( $n ); // This code is contributed // by ChitraNayal ?> |
Javascript
<script> // Javascript program to print all primes // less than N // function check whether a number // is prime or not function isPrime(n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for (let i = 2; i < n; i++) if (n % i == 0) return false ; return true ; } // Function to print primes function printPrime(n) { for (let i = 2; i <= n; i++) { if (isPrime(i)) document.write(i + " " ); } } // Driver Code let n = 7; printPrime(n); // This code is contributed by Mayank Tyagi </script> |
输出:
2 3 5 7
时间复杂性: O(N*N) A. 更好的方法 基于一个除数必须小于或等于√n、 所以我们只在√N
C++
// C++ program to print all primes // less than N #include <bits/stdc++.h> using namespace std; // function check whether a number // is prime or not bool isPrime( int n) { // Corner cases if (n <= 1) return false ; if (n <= 3) return true ; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false ; for ( int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false ; return true ; } // Function to print primes void printPrime( int n) { for ( int i = 2; i <= n; i++) { if (isPrime(i)) cout << i << " " ; } } // Driver Code int main() { int n = 7; printPrime(n); } |
JAVA
// Java program to print // all primes less than N import java.io.*; class GFG { // function check // whether a number // is prime or not static boolean isPrime( int n) { // Corner cases if (n <= 1 ) return false ; if (n <= 3 ) return true ; // This is checked so // that we can skip // middle five numbers // in below loop if (n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n; i = i + 6 ) if (n % i == 0 || n % (i + 2 ) == 0 ) return false ; return true ; } // Function to print primes static void printPrime( int n) { for ( int i = 2 ; i <= n; i++) { if (isPrime(i)) System.out.print(i + " " ); } } // Driver Code public static void main (String[] args) { int n = 7 ; printPrime(n); } } // This code is contributed // by anuj_67. |
C#
// C# program to print // all primes less than N using System; class GFG { // function check // whether a number // is prime or not static bool isPrime( int n) { // Corner cases if (n <= 1) return false ; if (n <= 3) return true ; // This is checked so // that we can skip // middle five numbers // in below loop if (n % 2 == 0 || n % 3 == 0) return false ; for ( int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false ; return true ; } // Function to print primes static void printPrime( int n) { for ( int i = 2; i <= n; i++) { if (isPrime(i)) Console.Write(i + " " ); } } // Driver Code public static void Main () { int n = 7; printPrime(n); } } // This code is contributed // by ChitraNayal |
Python3
# function to check if the number is # prime or not def isPrime(n) : # Corner cases if (n < = 1 ) : return False if (n < = 3 ) : return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 = = 0 or n % 3 = = 0 ) : return False i = 5 while (i * i < = n) : if (n % i = = 0 or n % (i + 2 ) = = 0 ) : return False i = i + 6 return True # print all prime numbers # less than equal to N def printPrime(n): for i in range ( 2 , n + 1 ): if isPrime(i): print (i, end = " " ) n = 7 printPrime(n) |
Javascript
<script> // Javascript program to print all primes // less than N // function check whether a number // is prime or not function isPrime(n) { // Corner cases if (n <= 1) return false ; if (n <= 3) return true ; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false ; for (let i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false ; return true ; } // Function to print primes function printPrime(n) { for (let i = 2; i <= n; i++) { if (isPrime(i)) document.write(i + " " ); } } // Driver Code let n = 7; printPrime(n); // This code is contributed by subhammahato348. </script> |
PHP
<?php // PHP program to print // all primes less than N // function check whether // a number is prime or not function isPrime( $n ) { // Corner cases if ( $n <= 1) return false; if ( $n <= 3) return true; // This is checked so that // we can skip middle five // numbers in below loop if ( $n % 2 == 0 || $n % 3 == 0) return false; for ( $i = 5; $i * $i <= $n ; $i = $i + 6) if ( $n % $i == 0 || $n % ( $i + 2) == 0) return false; return true; } // Function to print primes function printPrime( $n ) { for ( $i = 2; $i <= $n ; $i ++) { if (isPrime( $i )) echo $i . " " ; } } // Driver Code $n = 7; printPrime( $n ); // This code is contributed // by ChitraNayal ?> |
输出:
2 3 5 7
时间复杂性: O(N) 3/2 ) 这个 最佳解决方案 就是使用 埃拉托斯坦筛 时间复杂度为O(N*loglog(N))
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END