-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge07.c
83 lines (73 loc) · 1.73 KB
/
challenge07.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Find the 10,001st prime number */
/*And my second C code!!!!*/
/*C code was found to be functional with gcc compiler on ubuntu linux*/
#include<stdio.h>
// Inform user of program's purpose
void startDialogue(){
printf("This program aims to find prime number based upon position\n");
printf("For example, if I input '3' the third prime number will be returned\n");
printf("Please provide an integer\n");
}
// Get user input: which prime are we looking for?
int getInput(){
int number;
scanf("%d",&number);
printf("Your number is %d\n", number);
return number;
}
// Quickcheck quickly gets rid of numbers divisible by 2 and 5
int quickCheck(int n){
int maybeprime = 1;
if (n%2==0 || n%5==0){
maybeprime = 0;
}
if (n==2){
maybeprime = 1;
}
return maybeprime;
}
// Run after quickCheck
// Does a complete check to see if a number is prime
int fullCheck(int n){
int isprime = 0;
int ctr = 0;
for(int i=2;i<n;i++){
if (n%i == 0){
ctr = ctr + 1;
}
}
if (ctr == 0 || n==2){
isprime = 1;
}
return isprime;
}
// Get Prime number corresponding to user input
// Calls to quick check and full check in order to check if the number is prime
// Note: 2 is a prime number that is divisible by 2. Put controls in quick and full check functions
void getPrime(int startnumber){
int x = 0;
int n = 1;
int maybeprime = 0;
int isprime = 0;
while (x < startnumber+1){
maybeprime = quickCheck(n);
if (maybeprime != 0){
isprime = fullCheck(n);
if (isprime != 0){
x = x + 1;
if (x == startnumber){
printf("The prime you are looking for is %d\n", n);
}
}
}
n=n+1;
}
}
//Main is our container function
void main()
{
int startnumber;
startDialogue();
startnumber = getInput();
getPrime(startnumber);
}