-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex.8.14A.c
56 lines (43 loc) · 795 Bytes
/
ex.8.14A.c
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
54
55
56
#include <stdio.h>
#include <stdbool.h>
int n;
float guess = 1.0;
float absoluteValue (void)
{
float value = (guess * guess / (float) n) - 1.0;
if ( value < 0 )
value = -value;
return value;
}
float squareRoot (void)
{
float epsilon = .0001;
while ( absoluteValue () > epsilon )
guess = (guess + (float) n / guess) / 2.0;
return guess;
}
int prime (void)
{
if ( n == 2 ) {
return 1;
} else if ( n > 2 ) {
bool isPrime = true;
int i;
for ( i = 2; i <= squareRoot (); i += (i > 2) ? 2 : 1 )
if ( n % i == 0 )
return 0;
return 1;
} else {
return 0;
}
}
int main (void)
{
printf ("Which number do you want to check for prime? ");
scanf ("%i", &n);
if ( prime () )
printf ("%i is prime\n", n);
else
printf ("%i is not prime\n", n);
return 0;
}