Question

In: Computer Science

(Prime Numbers) An integer is said to be prime if it is divisible by only 1...

(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, as are 5 and 7, and 11 and 13. In your test program, use isPrime to determine and print all twin primes less than 1000. Display the output as follows:

(3, 5)

(5, 7)

Solutions

Expert Solution

FUNCTION

bool isPrime(int num)
{ int p=0;
for(int i = 2; i <= sqrt(num); i+=1)
if(num % i == 0)
p=1;
if(p==1)
return false;
else
return true;
}

Pseudocode

  1. START
  2. SET I = 2,P=0
  3. IF I < SqureRoot of NUM goto STEP 4 else goto STEP 5
  4. IF(NUM % I == 0) SET P=1
  5. IF(P==1) return false, ELSE return true.
  6. STOP

SOURCE CODE

#include <iostream>
#include<math.h>
using namespace std;

bool isPrime(int num)
{ int p=0;
for(int i = 2; i <= sqrt(num); i+=1)
if(num % i == 0)
p=1;
if(p==1)
return false;
else
return true;
}

int main()
{
bool flag;
int i;
for(i=2;i<1000;i++)
{
flag = isPrime(i);
if (flag == true )
{
flag = isPrime(i+2);
if (flag == true )
cout << "("<<i<<", "<< i+2<< ")\n";
}
}
return 0;
}

OUTPUT

(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
(101, 103)
(107, 109)
(137, 139)
(149, 151)
(179, 181)
(191, 193)
(197, 199)
(227, 229)
(239, 241)
(269, 271)
(281, 283)
(311, 313)
(347, 349)
(419, 421)
(431, 433)
(461, 463)
(521, 523)
(569, 571)
(599, 601)
(617, 619)
(641, 643)
(659, 661)
(809, 811)
(821, 823)
(827, 829)
(857, 859)
(881, 883)


SCREENSHOT

please give a upvote if u feel helpful.


Related Solutions

Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. Java
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. b) Modify the...
Show that an integer is divisible by 17 if and only if the integer formed by...
Show that an integer is divisible by 17 if and only if the integer formed by subtracting 5 times the last digit from the integer formed from the remaining digits is divisible by 17. [For example 442 is divisible by 17 since 44 – 5*2 = 34 is divisible by 17] course: discrete structure
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime...
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime number.
A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
isPrime Function. A prime number is a number that is only evenly divisible by itself and...
isPrime Function. A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. The...
Please prove 1. Every positive integer is a product of prime numbers. 2. If a and...
Please prove 1. Every positive integer is a product of prime numbers. 2. If a and b are relatively prime, and a|bc, then a|c. 3. The division algorithm for F[x]. Just the existence part only, not the uniqueness part
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume that we are given a list of all the prime numbers from 1 to 10,000, in sorted order in a text file called primes.txt: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,...
Write following program using Python: A positive integer greater than 1 is said to be prime...
Write following program using Python: A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that asks the user to enter a integer greater than 1, then displays all of the prime numbers that are less than or equal to the number entered. Last, create two files. One file will hold all of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT