Question

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cmath>
using namespace std ;



//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;
}



int main() {
int input = 0 ;
    cout<< "Prime numbers less than 1000 and of the form (2^n) + 1 are as follows :  "<< endl ;



    // to generate all integers less than 1000 of the form 2^n + 1
    for (int i = 1; i < 10  /* because 2^10  = 1024  greater than 1000 */ ; ++i) {
        int number =  (int) pow(2 , i) + 1  ;

      bool result =   check_prime(number);
        if( result == true ){
            cout<< pow(2 , i) + 1 << "\t";

        }


    }


    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Programming Use the following function to create a program that determines whether (n ^ 2...
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
  • c++ 2. Write a function to test primality Use the definition of prime number to guide...

    c++ 2. Write a function to test primality Use the definition of prime number to guide your implementation of the following function. After testing it for correctness, think about how the following result can be used to improve the efficiency. For any positive integers n, r, s, if n=rs, then r<=sqrt(n) or s<=sqrt(n) /* return true if n is prime return false if n is not prime Precondition: n>1 bool IsPrime (int n)

  • I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...

    I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Use c++ programming environment to implement and test a function with the following prototype: //the parameter...

    Use c++ programming environment to implement and test a function with the following prototype: //the parameter month and day should represent a valid date //return true if the date is between 3/8 and 10/31; false otherwise bool is_daylight(int month, int day); Following TDD (test-driven development), it is important to know the expected return values for different function calls. Function call Expected return Function call Expected return is_daylight(2, 20) false is_daylight(3, 7) false is_daylight(4, 1) true is_daylight(10, 31) true is_daylight(11, 1)...

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

  • Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program...

    Write a C++ program that computes the following series: sum = 1/firstPrime + 2/secondPrime+…..+1/nthPrime Your program should prompt the user to enter a number n. The program will compute and display sum based on the series defined above. firstPrime: is 2 secondPrime: the first prime number after 2 thirdPrime: the third prime number …. nth prime: the nth prime number Your program must be organized as follows: int main() { //prompt the user to enter n //read n from the...

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • In C programming, Modify the function Pop in the example so that it has the signature...

    In C programming, Modify the function Pop in the example so that it has the signature                                                 bool Pop(LIST *list, char *c) and returns false if the list is empty and returns true if not empty. On success it returns the value removed from the stack in the variable c. Modify the function CheckForBalance to accommodate this change and rerun the test program giving the same output as in the example. :here is the CheckForBalance example code, the rest of...

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

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