Explanation:
Below is the Java code for above problem with proper description provided within comments itself. Below code some sample output screenshots are attached.
If you need any other help for this Comment me below. Thanks
Java code :
import java.util.Scanner;
// create thread
class ThreadDemo extends Thread {
// local n
private int n;
ThreadDemo(int n) {
this.n = n;
}
// algorithm
public boolean[] printPrime(int n) {
boolean A[] = new boolean[n];
for (int i = 2; i < n; i++)
A[i] = true;
for (int i = 2; i < n; i++) {
if (A[i]) {
int count = 1;
for (int j = (int) Math.pow(i, 2); j < n;) {
A[j] = false;
j = (int) Math.pow(i, 2) + (count++) * i;
}
}
}
for (int i = 2; i < n; i++) {
if (A[i])
System.out.print(i + " ");
}
return A;
}
void printReversePrime(boolean A[]) {
for (int i = 2; i < n; i++) {
if (A[i] && reverse(i) < A.length && A[reverse(i)])
System.out.print(i + " ");
}
}
// reverse a number
int reverse(int n) {
String input = String.valueOf(n);
String result = "";
for (int i = input.length() - 1; i >= 0; i--) {
result = result + input.charAt(i);
}
int reversedInt = Integer.parseInt(result);
return reversedInt;
}
// run the thread
public void run() {
try {
// System.out.println("Thread: " + threadName);
System.out.print("Prime : ");
boolean A[] = this.printPrime(this.n);
System.out.print("\n\nReverese Prime : ");
new ThreadDemo(n).printReversePrime(A);
Thread.sleep(50);
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}
// test class
public class TestThread {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the n : ");
ThreadDemo T1 = new ThreadDemo(Integer.parseInt(sc.nextLine()));
T1.start();
sc.close();
}
}
Sample Output :
![, problems-Console × @Javadoc E. Declarationずsearch-Terminal眷Debug i. Display <terminated> TestThread lava Application] C:Pr](http://img.homeworklib.com/questions/81ff8d00-2bd8-11ec-9672-65a23de8ef73.png?x-oss-process=image/resize,w_560)
The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...
in visual studio build a masm program that prints out the
prime numbers in a array
L1001-Sieve of Eratosthenes Please use your textbook as a reference. Goal: Use what we have learned to generate prime numbers. Prime numbers have many applications in computer science and as such, efficient ways to discover prime numbers can be very useful. Mathematicians have been intrigued by the concept for ages including the Greek mathematician, Eratosthenes of Cyrene (famous for calculating the circumference o the...
Testing for a prime number by testing divisibility is a bit inefficient. One method to efficiently find prime numbers is to use what is called a “prime sieve” algorithm. Check out the following link to learn more about a famous instance of such an algorithm. https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes The Sieve of Eratosthenes is a method for finding all prime numbers up to a given number. Your task is to write a program that defines an array of 100 boolean values. Your...
PYTHON! The Sieve of Eratosthenes THANKS FOR
HELP!
A prime integer is any integer greater than 1 that is evenly
divisible only by itself and 1. The Sieve of Eratosthenes is a
method of finding prime numbers. It operates as follows:
Create a list with all elements initialized to 1 (true). List
elements with prime indexes will remain 1. All other elements will
eventually be set to zero.
Starting with list element 2, every time a list element is found...
Write a multithreaded a Java program that outputs prime numbers. This program should work as follows: The user will run the program and will enter a number on the command line, The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user.
Use phyton TheSieve of Eratosthonesis an algorithm to find all the prime numbers between 1 andsome integerN. It can be implemented with nestedforloops:(a) Make a list of all the integers from 2 throughN.(b) Cross off all the multiples of 2 (except for 2 itself). The smallest number that remains(after 2) is 3.(c) Cross off all the multiples of 3 (except for 3 itself). The smallest number that remainsis 5.(d) Cross off all the multiples of 5 (except for 5 itself)....
Write a multithreaded C program that outputs prime numbers. This program should work as follows: the user will run the program and will enter a number on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user. ADD "comments" to the source code please and a screenshot of the output with steps on how to compile
One way to find prime numbers less than n is to treat them as array indices. Mark each position as True initially, assuming that all numbers are prime. Then, starting with index 2, mark all multiples of 2 (greater than 2) as not prime (False). Repeat this for multiples of 3, then multples of 4, etc. When the algorithm terminates, only prime indices will still be True; everything else will be False. The following function takes an integer n as...
Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...
Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived. The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given...
Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...