Question

The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite lie., not prime) the multiples of each prime, starting with the multiples of 2 The sieve of Eratosthenes can be expressed in pseudocode, as follows: Input: an integer n Let A be an array of 8oo1ean values, indexed by integers 2 to n, initially all set to true. for t - 2, 3, 4, ..., not exceeding A if ?i] is true: for j. t, iat, t?+21, th31, , not exceeding n: ALJ)-false. Output: al1 t such that A[4) is true. This algorithm produces all primes <= n. It includes common optimization, which is to start enumerating the multiples of each prime i from P. The time complexity of this algorithm is Oin log log n). Write a multi-threaded program that outputs prime numbers. The program should work as follows: the user will run the program and enter a number on the command line. The program creates a thread that outputs all the prime numbers less than or equal to the number entered by the user. Also, create a separate thread that outputs this subset of the above primes that have the following property: the number that is derived by reversing the digits is also prime (e-g., 79 and 97).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in visual studio build a masm program that prints out the prime numbers in a array...

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

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

    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:...

    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 throug...

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

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

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

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

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

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

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT