SlideShare una empresa de Scribd logo
1 de 2
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;
// 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

Más contenido relacionado

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

SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfankkitextailes
 
Computer java programs
Computer java programsComputer java programs
Computer java programsADITYA BHARTI
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
An input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxAn input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxlauracallander
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfanjanacottonmills
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfaparnatiwari291
 

Similar a Please develop a Java program- using sieve of Eratosthenes- that print.docx (6)

SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
An input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxAn input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docx
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdfCountPositiveNumbersInArray.javapackage org.students;import java.pdf
CountPositiveNumbersInArray.javapackage org.students;import java.pdf
 

Más de ellenj4

The function and process whereby physicians- nurses- and social worker.docx
The function and process whereby physicians- nurses- and social worker.docxThe function and process whereby physicians- nurses- and social worker.docx
The function and process whereby physicians- nurses- and social worker.docxellenj4
 
The four components of aggregate expenditures are- Question 3 options-.docx
The four components of aggregate expenditures are- Question 3 options-.docxThe four components of aggregate expenditures are- Question 3 options-.docx
The four components of aggregate expenditures are- Question 3 options-.docxellenj4
 
The IS staff wants to know about important management tools built into.docx
The IS staff wants to know about important management tools built into.docxThe IS staff wants to know about important management tools built into.docx
The IS staff wants to know about important management tools built into.docxellenj4
 
The Inventive Co- is considering a new project- This project requires.docx
The Inventive Co- is considering a new project- This project requires.docxThe Inventive Co- is considering a new project- This project requires.docx
The Inventive Co- is considering a new project- This project requires.docxellenj4
 
The internal document that is prepared to notify the appropriate perso.docx
The internal document that is prepared to notify the appropriate perso.docxThe internal document that is prepared to notify the appropriate perso.docx
The internal document that is prepared to notify the appropriate perso.docxellenj4
 
The Impact of Technology on the Developing Child name 7 negative ways.docx
The Impact of Technology on the Developing Child name 7 negative ways.docxThe Impact of Technology on the Developing Child name 7 negative ways.docx
The Impact of Technology on the Developing Child name 7 negative ways.docxellenj4
 
The Homeland Security Act of 2002 is divided into 17 titles while brin.docx
The Homeland Security Act of 2002 is divided into 17 titles while brin.docxThe Homeland Security Act of 2002 is divided into 17 titles while brin.docx
The Homeland Security Act of 2002 is divided into 17 titles while brin.docxellenj4
 
The Homeland Security Act of 2002 is divided into 17 titles while brin (1).docx
The Homeland Security Act of 2002 is divided into 17 titles while brin (1).docxThe Homeland Security Act of 2002 is divided into 17 titles while brin (1).docx
The Homeland Security Act of 2002 is divided into 17 titles while brin (1).docxellenj4
 
The following table contains national income accounting data for a hyp.docx
The following table contains national income accounting data for a hyp.docxThe following table contains national income accounting data for a hyp.docx
The following table contains national income accounting data for a hyp.docxellenj4
 
The following items are taken from the financial statements of the Pos.docx
The following items are taken from the financial statements of the Pos.docxThe following items are taken from the financial statements of the Pos.docx
The following items are taken from the financial statements of the Pos.docxellenj4
 
The following program has already been written to populate its data ar.docx
The following program has already been written to populate its data ar.docxThe following program has already been written to populate its data ar.docx
The following program has already been written to populate its data ar.docxellenj4
 
The German philosopher Immanuel Kant condemned Utilitarianism as an im.docx
The German philosopher Immanuel Kant condemned Utilitarianism as an im.docxThe German philosopher Immanuel Kant condemned Utilitarianism as an im.docx
The German philosopher Immanuel Kant condemned Utilitarianism as an im.docxellenj4
 
The graph of temperature versus molar mass is given for the hydrides o.docx
The graph of temperature versus molar mass is given for the hydrides o.docxThe graph of temperature versus molar mass is given for the hydrides o.docx
The graph of temperature versus molar mass is given for the hydrides o.docxellenj4
 
The general role of financial intermediaries is to- A-Provide advice t (1).docx
The general role of financial intermediaries is to- A-Provide advice t (1).docxThe general role of financial intermediaries is to- A-Provide advice t (1).docx
The general role of financial intermediaries is to- A-Provide advice t (1).docxellenj4
 
The general role of financial intermediaries is to- A-Provide advice t.docx
The general role of financial intermediaries is to- A-Provide advice t.docxThe general role of financial intermediaries is to- A-Provide advice t.docx
The general role of financial intermediaries is to- A-Provide advice t.docxellenj4
 
The functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docx
The functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docxThe functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docx
The functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docxellenj4
 
The following information pertains to Naomi Campbell Company as of Dec.docx
The following information pertains to Naomi Campbell Company as of Dec.docxThe following information pertains to Naomi Campbell Company as of Dec.docx
The following information pertains to Naomi Campbell Company as of Dec.docxellenj4
 
The G Banking Group recently reported that it was offshoring (moving).docx
The G Banking Group recently reported that it was offshoring (moving).docxThe G Banking Group recently reported that it was offshoring (moving).docx
The G Banking Group recently reported that it was offshoring (moving).docxellenj4
 
Please do not answer if you-'re unsure of the answer- Thanks!.docx
Please do not answer if you-'re unsure of the answer- Thanks!.docxPlease do not answer if you-'re unsure of the answer- Thanks!.docx
Please do not answer if you-'re unsure of the answer- Thanks!.docxellenj4
 
Please describe the process of the Implementation of Role-based access.docx
Please describe the process of the Implementation of Role-based access.docxPlease describe the process of the Implementation of Role-based access.docx
Please describe the process of the Implementation of Role-based access.docxellenj4
 

Más de ellenj4 (20)

The function and process whereby physicians- nurses- and social worker.docx
The function and process whereby physicians- nurses- and social worker.docxThe function and process whereby physicians- nurses- and social worker.docx
The function and process whereby physicians- nurses- and social worker.docx
 
The four components of aggregate expenditures are- Question 3 options-.docx
The four components of aggregate expenditures are- Question 3 options-.docxThe four components of aggregate expenditures are- Question 3 options-.docx
The four components of aggregate expenditures are- Question 3 options-.docx
 
The IS staff wants to know about important management tools built into.docx
The IS staff wants to know about important management tools built into.docxThe IS staff wants to know about important management tools built into.docx
The IS staff wants to know about important management tools built into.docx
 
The Inventive Co- is considering a new project- This project requires.docx
The Inventive Co- is considering a new project- This project requires.docxThe Inventive Co- is considering a new project- This project requires.docx
The Inventive Co- is considering a new project- This project requires.docx
 
The internal document that is prepared to notify the appropriate perso.docx
The internal document that is prepared to notify the appropriate perso.docxThe internal document that is prepared to notify the appropriate perso.docx
The internal document that is prepared to notify the appropriate perso.docx
 
The Impact of Technology on the Developing Child name 7 negative ways.docx
The Impact of Technology on the Developing Child name 7 negative ways.docxThe Impact of Technology on the Developing Child name 7 negative ways.docx
The Impact of Technology on the Developing Child name 7 negative ways.docx
 
The Homeland Security Act of 2002 is divided into 17 titles while brin.docx
The Homeland Security Act of 2002 is divided into 17 titles while brin.docxThe Homeland Security Act of 2002 is divided into 17 titles while brin.docx
The Homeland Security Act of 2002 is divided into 17 titles while brin.docx
 
The Homeland Security Act of 2002 is divided into 17 titles while brin (1).docx
The Homeland Security Act of 2002 is divided into 17 titles while brin (1).docxThe Homeland Security Act of 2002 is divided into 17 titles while brin (1).docx
The Homeland Security Act of 2002 is divided into 17 titles while brin (1).docx
 
The following table contains national income accounting data for a hyp.docx
The following table contains national income accounting data for a hyp.docxThe following table contains national income accounting data for a hyp.docx
The following table contains national income accounting data for a hyp.docx
 
The following items are taken from the financial statements of the Pos.docx
The following items are taken from the financial statements of the Pos.docxThe following items are taken from the financial statements of the Pos.docx
The following items are taken from the financial statements of the Pos.docx
 
The following program has already been written to populate its data ar.docx
The following program has already been written to populate its data ar.docxThe following program has already been written to populate its data ar.docx
The following program has already been written to populate its data ar.docx
 
The German philosopher Immanuel Kant condemned Utilitarianism as an im.docx
The German philosopher Immanuel Kant condemned Utilitarianism as an im.docxThe German philosopher Immanuel Kant condemned Utilitarianism as an im.docx
The German philosopher Immanuel Kant condemned Utilitarianism as an im.docx
 
The graph of temperature versus molar mass is given for the hydrides o.docx
The graph of temperature versus molar mass is given for the hydrides o.docxThe graph of temperature versus molar mass is given for the hydrides o.docx
The graph of temperature versus molar mass is given for the hydrides o.docx
 
The general role of financial intermediaries is to- A-Provide advice t (1).docx
The general role of financial intermediaries is to- A-Provide advice t (1).docxThe general role of financial intermediaries is to- A-Provide advice t (1).docx
The general role of financial intermediaries is to- A-Provide advice t (1).docx
 
The general role of financial intermediaries is to- A-Provide advice t.docx
The general role of financial intermediaries is to- A-Provide advice t.docxThe general role of financial intermediaries is to- A-Provide advice t.docx
The general role of financial intermediaries is to- A-Provide advice t.docx
 
The functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docx
The functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docxThe functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docx
The functional group RCO2R-' is characteristic of a(n) ____- 1) 2) 3).docx
 
The following information pertains to Naomi Campbell Company as of Dec.docx
The following information pertains to Naomi Campbell Company as of Dec.docxThe following information pertains to Naomi Campbell Company as of Dec.docx
The following information pertains to Naomi Campbell Company as of Dec.docx
 
The G Banking Group recently reported that it was offshoring (moving).docx
The G Banking Group recently reported that it was offshoring (moving).docxThe G Banking Group recently reported that it was offshoring (moving).docx
The G Banking Group recently reported that it was offshoring (moving).docx
 
Please do not answer if you-'re unsure of the answer- Thanks!.docx
Please do not answer if you-'re unsure of the answer- Thanks!.docxPlease do not answer if you-'re unsure of the answer- Thanks!.docx
Please do not answer if you-'re unsure of the answer- Thanks!.docx
 
Please describe the process of the Implementation of Role-based access.docx
Please describe the process of the Implementation of Role-based access.docxPlease describe the process of the Implementation of Role-based access.docx
Please describe the process of the Implementation of Role-based access.docx
 

Último

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Último (20)

Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

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

  • 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. // 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