-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex.8.10.c
More file actions
53 lines (42 loc) · 757 Bytes
/
Copy pathex.8.10.c
File metadata and controls
53 lines (42 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdbool.h>
float absoluteValue (float n)
{
if ( n < 0 )
n = -n;
return n;
}
float squareRoot (float n)
{
float guess = 1.0;
float epsilon = .0001;
while ( absoluteValue ((guess * guess / n) - 1) > epsilon )
guess = (guess + n / guess) / 2.0;
return guess;
}
int prime (n)
{
if ( n == 2 ) {
return 1;
} else if ( n > 2 ) {
bool isPrime = true;
int i;
for ( i = 2; i <= squareRoot ((float) n); i += (i > 2) ? 2 : 1 )
if ( n % i == 0 )
return 0;
return 1;
} else {
return 0;
}
}
int main (void)
{
int n;
printf ("Which number do you want to check for prime? ");
scanf ("%i", &n);
if ( prime (n) )
printf ("%i is prime\n", n);
else
printf ("%i is not prime\n", n);
return 0;
}