Question

Define in Scheme an infinite stream consisting of twin prime numbers. All primes ? with the...

Define in Scheme an infinite stream consisting of twin prime numbers. All primes ? with the property that ? + 2 or ? − 2 are also prime should be included, each one only once. Hint: Apply the Sieve of Eratosthenes.

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

ANSWER:

Prime Number : Number that is divisible by only itself and 1 is called prime number.

Twin Prime : Pair of prime numbers having difference of 2.
let's say p1 and p2 are two prime numbers with p2 > p1
the they are said to be twin prime if p2 - p1 = 2.
example : p1 = 3, p2 = 5
p1 = 11 p2 = 13


Algorithm Design : We use Sieve of Eratosthenes to generate the infinite stream of twin primes.
1) we will find all prime numbers.
2) we will iterate through the list to check for twin primes.
3) condition for twin primes :
p1, p2 are prime and p2 - p1 = 2

c++ implementation :

#include <bits/stdc++.h>
using namespace std;

/ define infinte a number for the program as /
// we can not can not run the program for infinity
#define infinte 100000

// map to store the twin prime numbers
map <int, int> twinPrime;
map <int, int> :: iterator it;


// Sieve of eratosthenes to calculate the twin prime numbers
// and store them into map
void TwinPrime() {
/ Create a boolean array isprime[] /
/ and initialize all entries till infinite it as true. /
/ isprime[p] will be false if p is not prime /
/ isprime[p] will be true if p is prime /
bool isprime[infinte];

for(int i = 1;i <= infinte;i++) {
isprime[i] = true;
}


for(int i = 2;i*i <= infinte;i++) {

// for every number i which is factor of it's
// multiples, mark it's multiple as false
// as they can be factorized into i
// so they are not prime

if(isprime[i] == true) {
// multiples of i
for(int j = i * 2;j <= infinte;j += i) {
// mark the multipels as false
isprime[j] = false;
}
}

}
int j = 0;

// check for the twin primes
// if i and i+2 are primes then
// they will be twin primes
// as i+2 - i = 2

for(int i = 2;i <= infinte - 2;i++) {
if(isprime[i] && isprime[i+2]) {
// store into map
twinPrime[i]++;
twinPrime[i+2]++;
}
}
// print the twin primes.
cout << "Twin Prime Numbers are : ";
for(it = twinPrime.begin();it != twinPrime.end();it++) {
cout << it->first << " ";
}
}

// driver main function
int main()
{
// call the TwinPrime fucntion to find
// all the twin prime numbers
TwinPrime();

return 0;
}

images :

Output Screenshot :

Add a comment
Know the answer?
Add Answer to:
Define in Scheme an infinite stream consisting of twin prime numbers. All primes ? with the...
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
  • Define in Scheme an infinite stream consisting of all pairs of twin prime numbers, i.e. all...

    Define in Scheme an infinite stream consisting of all pairs of twin prime numbers, i.e. all pairs of prime numbers differing by 2, listed in increasing order according to the first number of a pair, in every pair the first item being smaller than the second item. Hint: Apply the sieve of Eratosthenes in order to construct a predicate is-prime?

  • The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to...

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

  • Scheme Language: Define a function nondecreastream, which takes in a stream of numbers and outputs a...

    Scheme Language: Define a function nondecreastream, which takes in a stream of numbers and outputs a stream of lists, which overall has the same numbers in the same order, but grouped into segments that are non-decreasing. For example, if the input is a stream containing elements 1 2 3 4 1 2 3 4 1 1 1 2 1 1 0 4 3 2 1 ... the output should contain elements (1 2 3 4) (1 2 3 4) (1...

  • Use a Mathlab program using the while/for command and run it for the following exercise. 20. A twin primes is a pair of prime numbers such that the difference between them is2 (for example, 17 an...

    Use a Mathlab program using the while/for command and run it for the following exercise. 20. A twin primes is a pair of prime numbers such that the difference between them is2 (for example, 17 and 19). Write a computer program that finds all the twin primes between 10 and 500. The program displays the results in a two- column matrix in which each row is a twin prime. Do not use MATLAB's built-in function is prime. Use a Mathlab...

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

  • 8. Define (n) to be the number of positive integers less than n and n. That...

    8. Define (n) to be the number of positive integers less than n and n. That is, (n) = {x e Z; 1 < x< n and gcd(x, n) = 1}|. Notice that U (n) |= ¢(n). For example U( 10) = {1, 3,7, 9} and therefore (10)= 4. It is well known that (n) is multiplicative. That is, if m, n are (mn) (m)¢(n). In general, (p") p" -p Also it's well known that there are relatively prime, then...

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

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

  • Define a function that creates a list of all the numbers that are factors of the...

    Define a function that creates a list of all the numbers that are factors of the user's number. For example, if the function is called factor, factor(36) should return [1, 2, 3, 4, 6, 9, 12, 18, 36]. The numbers in your list should be sorted from least to greatest, and 1 and the original number should be included. Remember to consider negative numbers as well as 0. Bonus: Have the program print the factors of the users number in...

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
ADVERTISEMENT