-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequencer.c
139 lines (124 loc) · 2.67 KB
/
sequencer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <stdio.h>
#include <stdlib.h>
#define MAXINPUT 3
#include "printfcolor.h"
#define COLORS
typedef enum {ARITHMETIC,GEOMETRIC,PRIME,UNKNOWN} Sequence;
unsigned int i,position,count;
float raised,number,term,numbers[10];
void
getInput()
{
printf("Kindly input ");
printfc_fg(RED, "first three ");
printf("numbers of sequence ");
printfc_fg(YELLOW, "below ");
printf("using <");
printfc_fg(GREEN, "spacebar");
printf("> to separate\n");
for (i = 0; i < MAXINPUT; i++){
if(!scanf("%f",&number)){
printfc_fg(RED, "ERROR: ");
printf("Unable to get input\n");
exit(EXIT_FAILURE);
}
numbers[i] = number;
}
}
unsigned int
checkPrime
(unsigned int number)
{
count = 1;
for(i=2; i<=number/2; i++){
if(number%i == 0){
count=0;
break;
}
}
if(number == 1) count = 0;
return count;
}
Sequence
determineSequence()
{
numbers[MAXINPUT] = numbers[MAXINPUT - 1] - numbers[MAXINPUT - 2];
numbers[MAXINPUT + 1] = numbers[MAXINPUT - 2] - numbers[MAXINPUT - 3];
if (numbers[MAXINPUT] == numbers[MAXINPUT + 1])return ARITHMETIC;
numbers[MAXINPUT] = numbers[MAXINPUT - 1]/numbers[MAXINPUT - 2];
numbers[MAXINPUT + 1] = numbers[MAXINPUT - 2]/numbers[MAXINPUT - 3];
if (numbers[MAXINPUT] == numbers[MAXINPUT + 1])return GEOMETRIC;
if (checkPrime((unsigned int)numbers[0]))return PRIME;
else return UNKNOWN;
}
void
prompt()
{
printfc_fg(MAGENTA, "Position ");
printf("of term: ");
if(!scanf("%u",&position)){
printfc_fg(RED, "ERROR: ");
printf("Unable to get input\n");
exit(EXIT_FAILURE);
}
}
float
raise
(float base, unsigned int exponent)
{
raised = 1;
for (i = 0; i < exponent; i++) raised *= base;
return raised;
}
/*=======================WIP=======================
unsigned int
primeAtN(unsigned int number,unsigned int position)
{
i = 0;
while(i != position){
if(checkPrime(number)){
number++;
i++;
}
else number++;
}
return number;
}
==================================================*/
int
sequencer()
{
switch (determineSequence()){
case ARITHMETIC:
prompt();
term = numbers[0] + (position - 1) * numbers[MAXINPUT];
break;
case GEOMETRIC:
prompt();
term = numbers[0] * raise(numbers[MAXINPUT],position - 1);
break;
case PRIME:
prompt();
printfc_fg(RED, "WIP:");
printf("prime number finder not in its prime yet ;)\n");
return 0;
case UNKNOWN:
printfc_fg(YELLOW, "Sorry,");
printf(" could ");
printfc_fg(RED, "not");
printf("determine type of");
printfc_fg(BLUE, " sequence\n");
return 0;
}
printfc_fg(BLUE, "Term ");
printf("is: ");
printfc_fg(GREEN, "%g\n", term);
return 0;
}
int
main()
{
getInput();
sequencer();
return 0;
}