Question

Write a C++ function that inputs a positive integer and determines whether it is prime using...

Write a C++ function that inputs a positive integer and determines whether it is prime using trial division and find as many primes of the form n2+1 , where n is a positive integer, as you can.

c++/discrete structure

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

The basic idea of trial division method is to check if a number, num, has factors. This can be checked by dividing the number by every other number (excluding 1 and the number itself).

Code to copy:

#include <iostream>

using namespace std;

int main()

{

// Declare an int variable for the user input

int num;

// Initialize a flag variable to zero

int flag = 0;

// Prompt user to enter any positive integer

cout<<"Enter a positive integer greater than 1:\n";

// Read the user input

cin>>num;

// The loop will check if the number entered

// has a factor.

// Loop i from 2 to num-1

for(int i = 2; i < num; i++)

{

// Check if num is divisible by i

if(num % i == 0)

{

// Assign flag equal to 1 if a factor is found

flag = 1;

// end loop as soon as flag is equal to 1

break;

}

}

// Check if flag is equal to zero

if(flag == 0)

{

// Display the number is prime

cout<<"\nThe number, "<<num<<", is a prime number"<<endl;

}

else

{

// Display the number is not

cout<<"\nThe number, "<<num<<", is not a prime number"<<endl;

}

// As it is not possible to run a loop infinitely and no upper limit is mentioned in the question, the loop will check till the number enter by the user at the beginning of the program

int x;

bool f = 0;

for(int n=1; n<num; n++)

{

// Assign the number of the form (n*n)+1 to x

x = (n*n)+1;

// Check if the number x is a prime number

for(int m=2; m<x; m++)

{

// Check if num is divisible by i

if(x % m == 0)

{

// Assign flag equal to 1 if a factor is found

f = 1;

// end loop as soon as flag is equal to 1

break;

}

}

// Check if flag is equal to zero

if(f == 0)

{

// Display the numbers of that form

cout<<x<<" ";

}

}

return 0;

}

Screenshot of the code:

#include <iostream> using namespace std; int main() // Declare an int variable for the user input int num; // Initialize a fl

// As it is not possible to run a loop infinitely and no upper limit is mentioned in the question, the loop will check till t

Sample Output:

Enter a positive integer greater than 1: 29 The number, 29, is a prime number 2 5

Add a comment
Know the answer?
Add Answer to:
Write a C++ function that inputs a positive integer and determines whether it is prime using...
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
  • Part 1: Write a C++ function that inputs a positive integer and determines whether it is...

    Part 1: Write a C++ function that inputs a positive integer and determines whether it is prime using trial division. Part 2: Use the function from part 1 to make a C++ program that finds as many primes of the form n2 + 1, where n is a positive integer, as you can.

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • python language Primes Write a program primes.py that reads a positive integer from standard input, and...

    python language Primes Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime A prime number is a positive integer that is greater than 1, an e can be divided exactly (without leaving a rerm or itself 1 $ python3 primes. py 2 one positive integer please: 21 3 21 is not prime! 5 S python3 primes. py 6 one positive integer please 7 23 is prime

  • In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and...

    In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...

  • Write a method that determines if a given non-negative integer is a prime number, use the...

    Write a method that determines if a given non-negative integer is a prime number, use the following method header: public static boolean isprime(int n) Prime numbers are integers that are divisible only by 1 and themselves; for example, 2, 5, 7, and 13 are prime numbers, while 15, 16, and 20 are not. By definition, 0 and 1 are not prime numbers. To test your method, write a simple program that asks the user to enter a number and then...

  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

  • Write a Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • 3. Primes primes.py We're going to solve a popular interview question! Write a function that checks...

    3. Primes primes.py We're going to solve a popular interview question! Write a function that checks whether a number is prime. Handle the case where the argument is not a positive integer. Next, write a function that returns the first N primes. Can you optimize your code? Note that any number that is not prime is always divisible by at least one prime that comes before it. Ensure your program passes the tests provided.

  • A prime number is a positive integer whose positive integer divisors are 1 and the number....

    A prime number is a positive integer whose positive integer divisors are 1 and the number. E.g. 17 is a prime number -23 is a not prime number 28 is not a prime number -45 is not a prime number The only even prime number is 2 Write a boolean function, isPrime that return true if its integer parameter is a prime and false if its integer parameter is not a prime number. A simple prime number algorithm is if...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

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