Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19b2732

Browse files
Implement Sieve of Eratosthenes and prime number checker
1 parent 2cc668a commit 19b2732

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import java.util.Scanner;
3+
4+
public class SieveOfEratosthenes {
5+
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
System.out.print("Findes prime upto: ");
9+
int num = sc.nextInt();
10+
11+
boolean[] isPrime = new boolean[num + 1];
12+
13+
for (int i = 2; i <= num; i++) {
14+
isPrime[i] = true;
15+
16+
}
17+
18+
isPrime[0] = false;
19+
isPrime[1] = false;
20+
21+
// Start from 2 and mark all multiples as not prime
22+
for (int i = 2; i * i <= num; i++) {
23+
if (isPrime[i]) {
24+
for (int j = i * i; j <= num; j += i) {
25+
isPrime[j] = false;
26+
}
27+
}
28+
}
29+
30+
System.out.println("Prime numbers up to " + num + ":");
31+
32+
for (int i = 2; i < num; i++) {
33+
if (isPrime[i]) {
34+
System.out.println(i + " ");
35+
}
36+
}
37+
38+
}
39+
40+
}

‎Basic/Prime_Number/prime_Checker.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import java.util.Scanner;
3+
4+
public class prime_Checker {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.print("Enter the number: ");
8+
int num = sc.nextInt();
9+
// System.out.println(Math.sqrt(4));
10+
11+
if (isPrime(num)) {
12+
System.out.println(num+ " is Prime Number");
13+
} else {
14+
System.out.println(num + " is not Prime ");
15+
}
16+
}
17+
18+
static boolean isPrime(int num)
19+
{
20+
if (num <= 1)
21+
return false;
22+
23+
if (num == 2 )
24+
return true;
25+
26+
for (int i = 2; i <= Math.sqrt(num); i++) {
27+
if (num % i == 0) {
28+
return false;
29+
}
30+
}
31+
32+
return true;
33+
}
34+
}

‎Basic/Prime_Number/print_n_number.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import java.util.Scanner;
3+
4+
public class print_n_number {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.print("Enter the N number to check for prime numbers: ");
8+
int num = sc.nextInt();
9+
10+
System.out.println("Prime numbers up to " + num + " : ");
11+
for (int i = 2; i <= num; i++) {
12+
if (isPrimeCheck(i)) {
13+
System.out.println(i + " ");
14+
}
15+
}
16+
System.out.println();
17+
}
18+
19+
static boolean isPrimeCheck(int num) {
20+
if (num <= 1)
21+
return false;
22+
if (num == 2)
23+
return true;
24+
25+
for (int i = 2; i <= Math.sqrt(num); i++) {
26+
if (num % 2 == 0) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /