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
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:
Sample Output:
Write a C++ function that inputs a positive integer and determines whether it is prime using...
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 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 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 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. 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 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 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...
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 function that determines whether its parameter, which is an integer number has a specific mathematical characteristic. Then, call the function from inside a main function. The mathematical characteristic will be given to you (*), along with some examples. this is all that was given
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; }