Question: Java Homework 06 B ased on the program Primes.java below, develop a Java program for writing, in a text file named primes_n.txt, the prime numbers

Java Homework 06

Based on the program Primes.java below, develop a Java program for writing, in a text file named primes_n.txt, the prime numbers smaller than n.

The syntax for the execution of the program will be;

$ java Primes n

For instance, the program execution $java Primes 10, will generate the text file primes_10.txt with the following content;

Total number of primes smaller than 10: 4

1: 2

2: 3

3: 5

4 7

Concepts in practice: Text files in java

Files to be used:

Primes.java

//Primes.java

/** * A program in Java with simple optimisation to find prime numbers. */ public class Primes{ public static boolean isPrime(int n) { if (n<=1) return false; if (n==2) return true; if (n%2==0) return false; for (int i=3; i<=Math.sqrt(n); i+=2) if (n%i==0) return false; return true; } public static void main(String[] args) { if (args.length==0){ System.out.println(" Find Prime Numbers smaller than n, n in [0..2^31-1]"); System.out.println(" Syntax required: java Primes n"); System.exit(0); } for (int n=2; n<=Integer.parseInt(args[0]); n++) if (isPrime(n)) System.out.println(n); } } 

Files to deliver:

Primes.java, primes_1000.txt

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!