Question

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
from 2 to num-1. If any trial divides num, then num is not prime. But you only have to
test up to (and including) the integer square root of num. Say that num=a*b. Then if
a is larger than the square root, b must be smaller than the square root and has
already been tested.
Use your own function isqrt() for computing the integer square root. You will need
a prototype for this in isPrime.c
C:\Source\>gcc primeTester.c isqrt.c isPrime.c
C:\Source\>.\a.exe
Enter num: 223
223 is a prime

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

isPrime.c

#include <stdio.h>

#include <math.h>

int isPrime(long num) {

if (num <= 0)

return 0;

if (num == 2)

return 1;

if (num % 2 == 0)

return 0;

for (int i=3; i<sqrt(num); i+=2) {

if (num % i == 0) {

return 0;

}

}

return 1;

}

driver.c

#include <stdio.h>

#include <math.h>

#include "isPrime.c"

// driver code

int main() {

long num;

printf("Enter a number: ");

scanf("%ld", &num);

if (isPrime(num) == 1) {

printf("%ld is a prime.", num);

} else {

printf("%ld is not a prime.", num);

}

}

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) Enter a number: 223 223 is a prime.​​​​​​​

Add a comment
Know the answer?
Add Answer to:
In ASCII C programming write a function that determines if an integer is prime. A prime...
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
  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

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

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

  • 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

  • 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 prime number is a number that is only evenly divisible by itself and 1. for...

    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 name isPrime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. use this functuion in a...

  • C++, data structure An integer number’s proper divisor is any positive integer that divides the number...

    C++, data structure An integer number’s proper divisor is any positive integer that divides the number without remainder and is less than the number. Neither zero nor any negative number is a proper divisor. Write a function returns true if its second parameter is a proper divisor of its first parameter. The function’s prototype is bool properDivisor(int number, int candidate) Write a function that returns the sum of a number’s proper divisors. The function’s prototype is int properDivisorSum(int number); Use...

  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that...

    IN PYHTON CODE Question #3 Produce a function prime_factors.dict that has one integer variable n that is assumed to be greater than 1. The function will return a dictionary with keys the integers from 2 to n, inclusive, and with values the set of prime factors of each key. So, the command prime_factors.dict (8) will return the dictionary 2 123,3: 3),4 2),5 (53,6 2,3),7:7),8 {2)) Note that even though the prime number 2 appears twice in the prime fac- torization...

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

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