Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Execution time for prime numbers) Write a program that will obtain the executio

ID: 3560161 • Letter: #

Question

(Execution time for prime numbers) Write a program that will obtain the
execution time for finding all of the prime numbers that are less than 8,000,000,
10,000,000, 12,000,000, 14,000,000, 16,000,000, and 18,000,000 using the
algorithms in Listings 16.4

Write a program that prompts the user to enter two strings and tests whether the second string is a substring in the first string. (Don't use the find method in that str class.)Analyze the time complexity of your algorithm. Here is a sample run of the program.

Explanation / Answer

import java.util.Scanner;

/**
* Simple Java program to print prime numbers from 1 to 100 or any number.
* A prime number is a number which is greater than 1 and divisible
* by either 1 or itself.
*/

public class PrimeNumberExample {

public static void main(String args[]) {

//get input till which prime number to be printed
System.out.println("Enter the number till which prime number to be printed: ");
int limit = new Scanner(System.in).nextInt();

//printing primer numbers till the limit ( 1 to 100)
System.out.println("Printing prime number from 1 to " + limit);
for(int number = 2; number<=limit; number++){
//print prime numbers only
if(isPrime(number)){
System.out.println(number);
}
}

}

/*
* Prime number is not divisible by any number other than 1 and itself
* @return true if number is prime
*/

public static boolean isPrime(int number){
for(int i=2; i<number; i++){
if(number%i == 0){
return false; //number is divisible so its not prime
}
}
return true; //number is prime now
}
}

Output:
Enter the number till which prime number to be printed:
20
Printing prime number from 1 to 20
2
3
5
7
11
13
17
19