Question

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.

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include<iostream>

using namespace std;

//problem 1

//finding prime or not using trial division method

bool check_prime(int n)//returns true if n is prime

{

int i=2;

while(i*i < n)

{

if(n%i==0)return false;// because we have found a divisor

i++;

}

return true;

}

//problem2

//checks 2p-1 is prime or not for all primes not exceeding 100.

//problem3

//assuming n2 +1 is n^2 +1

//finding as many primes as we can

//which are of from n^2 +1

void check_primes_prob3()

{

int i=2;//taking 1 is not prime

while(i<10000)//mex this is only

{

//then checking fro n^2+1 is prime or not

if(check_prime(i*i +1))//finding and printing all such primes

{

cout<<"For "<<i<<" :"<<i<<"^"<<2<<"+1 :"<<i*i+1<<" is prime\n";

}

i++;

}

}

int main()

{

check_primes_prob3();

return 0;

}

Kindly revert for any queries

Thanks.

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

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

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • C++ Programming Use the following function to create a program that determines whether (n ^ 2...

    C++ Programming Use the following function to create a program that determines whether (n ^ 2 + 1) is prime for each of the primes not exceeding 1000. bool check_prime(int input) { for(int i = 2; i <= sqrt(input); ++i) {    if(input % i == 0)    {        return false;    } } return true; }

  • In C++ Write a function that receives an integer array with its size and determines whether...

    In C++ Write a function that receives an integer array with its size and determines whether the array is already sorted in increasing order. Write a test program (main function) that prompts the use to enter a list and displays whether the list is sorted or not. The first number in the input indicates the size of the list. Assume the maximum list size is 20.

  • In C++ Write a function that receives an integer array with its size and determines whether...

    In C++ Write a function that receives an integer array with its size and determines whether the array is already sorted in increasing order. Write a test program (main function) that prompts the use to enter a list and displays whether the list is sorted or not. The first number in the input indicates the size of the list. Assume the maximum list size is 20.

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

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

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

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