GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Others/SieveOfEratosthenes.java at master · feiteng/Java Expand file tree/ Copy pathSieveOfEratosthenes.javaMore file actions 49 lines (38 loc) · 1.09 KB / Copy pathSieveOfEratosthenes.javaFile metadata and controls49 lines (38 loc) · 1.09 KB 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 38 39 40 41 42 43 44 45 46 47 48 49 package Others; /** * @author Varun Upadhyay (https://github.com/varunu28) */ public class SieveOfEratosthenes { /** * This method implements the Sieve of Eratosthenes Algorithm * * @param n The number till which we have to check for prime * Prints all the prime numbers till n **/ public static void findPrimesTillN(int n) { int[] arr = new int[n + 1]; for (int i = 0; i <= n; i++) { arr[i] = 1; } arr[0] = arr[1] = 0; for (int i = 2; i <= Math.sqrt(n); i++) { if (arr[i] == 1) { for (int j = 2; i * j <= n; j++) { arr[i * j] = 0; } } } for (int i = 0; i < n + 1; i++) { if (arr[i] == 1) { System.out.print(i + " "); } } System.out.println(); } // Driver Program public static void main(String[] args) { int n = 100; // Prints 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 findPrimesTillN(n); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |