Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

# Please develop a Java program- using sieve of Eratosthenes- that print.docx

Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Próximo SlideShare
Java -lec-5
Cargando en…3
×

1 de 2 Anuncio

# Please develop a Java program- using sieve of Eratosthenes- that print.docx

Please develop a Java program, using sieve of Eratosthenes, that prints the last 100 prime numbers that are smaller than N, where N=1000.
Solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LastPrimeNumbers {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// initially assume all integers are prime
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter N Value :\");
int n = scan.nextInt();
List list = checkPrimes(n);
System.out.println(\"Prime Number are :\");
for(int i=list.size()-1; i>list.size()-1-100; i--){
System.out.print(list.get(i)+\" \");
}
}
public static List<Integer> checkPrimes(int n) {
List<Integer> list = new ArrayList<Integer>();
boolean [] status = new boolean [n + 1];
status[1] = true;
// Mark all composite numbers
for (int i = 2; i <= n; i++) {
if (!status[i]) {
// \'i\' is a prime number
int multiple = 2;
while (i * multiple <= n) {
status [i * multiple] = true;
multiple++;
}
}
}
return list;
}
}
Output:
Enter N Value :
1000
Prime Number are :
997 991 983 977 971 967 953 947 941 937 929 919 911 907 887 883 881 877 863 859 857 853 839 829 827 823 821 811 809 797 787 773 769 761 757 751 743 739 733 727 719 709 701 691 683 677 673 661 659 653 647 643 641 631 619 617 613 607 601 599 593 587 577 571 569 563 557 547 541 523 521 509 503 499 491 487 479 467 463 461 457 449 443 439 433 431 421 419 409 401 397 389 383 379 373 367 359 353 349 347
.

Please develop a Java program, using sieve of Eratosthenes, that prints the last 100 prime numbers that are smaller than N, where N=1000.
Solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LastPrimeNumbers {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// initially assume all integers are prime
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter N Value :\");
int n = scan.nextInt();
List list = checkPrimes(n);
System.out.println(\"Prime Number are :\");
for(int i=list.size()-1; i>list.size()-1-100; i--){
System.out.print(list.get(i)+\" \");
}
}
public static List<Integer> checkPrimes(int n) {
List<Integer> list = new ArrayList<Integer>();
boolean [] status = new boolean [n + 1];
status[1] = true;
// Mark all composite numbers
for (int i = 2; i <= n; i++) {
if (!status[i]) {
// \'i\' is a prime number
int multiple = 2;
while (i * multiple <= n) {
status [i * multiple] = true;
multiple++;
}
}
}
return list;
}
}
Output:
Enter N Value :
1000
Prime Number are :
997 991 983 977 971 967 953 947 941 937 929 919 911 907 887 883 881 877 863 859 857 853 839 829 827 823 821 811 809 797 787 773 769 761 757 751 743 739 733 727 719 709 701 691 683 677 673 661 659 653 647 643 641 631 619 617 613 607 601 599 593 587 577 571 569 563 557 547 541 523 521 509 503 499 491 487 479 467 463 461 457 449 443 439 433 431 421 419 409 401 397 389 383 379 373 367 359 353 349 347
.

Anuncio
Anuncio

## Más Contenido Relacionado

Anuncio

### Please develop a Java program- using sieve of Eratosthenes- that print.docx

1. 1. Please develop a Java program, using sieve of Eratosthenes, that prints the last 100 prime numbers that are smaller than N, where N=1000. Solution LastPrimeNumbers.java import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class LastPrimeNumbers { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // initially assume all integers are prime Scanner scan = new Scanner(System.in); System.out.println("Enter N Value :"); int n = scan.nextInt(); List list = checkPrimes(n); System.out.println("Prime Number are :"); for(int i=list.size()-1; i>list.size()-1-100; i--){ System.out.print(list.get(i)+" "); } } public static List<Integer> checkPrimes(int n) { List<Integer> list = new ArrayList<Integer>(); boolean [] status = new boolean [n + 1]; status[1] = true;
2. 2. // Mark all composite numbers for (int i = 2; i <= n; i++) { if (!status[i]) { // 'i' is a prime number list.add(i); int multiple = 2; while (i * multiple <= n) { status [i * multiple] = true; multiple++; } } } return list; } } Output: Enter N Value : 1000 Prime Number are : 997 991 983 977 971 967 953 947 941 937 929 919 911 907 887 883 881 877 863 859 857 853 839 829 827 823 821 811 809 797 787 773 769 761 757 751 743 739 733 727 719 709 701 691 683 677 673 661 659 653 647 643 641 631 619 617 613 607 601 599 593 587 577 571 569 563 557 547 541 523 521 509 503 499 491 487 479 467 463 461 457 449 443 439 433 431 421 419 409 401 397 389 383 379 373 367 359 353 349 347