Anonymous
#include <stdio.h> #include <math.h> #include <stdlib.h> //=================== Prototype function =============// int isPrimeRecurs2(int n, int i); int isPrimeRecurs(int n) { if (n <= 1) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; // return isPrimeRecurs2(n, 3); } int isPrimeRecurs2(int n, int i) { //check stop conditions if (i > floor(sqrt(n))) return 1; if (n % i == 0) return 0; // simulate loop iteration and increment loop counter return isPrimeRecurs2(n, i + 2); } //============ Main here ===================// int main() { printf("Please enter a number "); int n; scanf("%i", &n); int result = isPrimeRecurs(n); printf("%s\n", result ? "prime" : "not prime"); printf("result=%i\n", result); }
Anonymous
this is the code
Mihail
I'm really not sure what's the point of those callLoop functions and stuff
Mihail
Let's go back to the old code
Mihail
This code
Anonymous
it is to call the recursion
Mihail
Add a check if it's the current number
Mihail
That's why your program failed
Mihail
It was checking 7 % 7 == 0
Anonymous
vehlwn just fixed it and is this one
Mihail
And returning as not prime
Mihail
Oh wait
Anonymous
ok this is the code
Anonymous
#include <stdio.h> #include <math.h> #include <stdlib.h> int isPrimeRecurs2(int n, int i); int isPrimeRecurs(int n) { if (n <= 1) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; return isPrimeRecurs2(n, 3); } int isPrimeRecurs2(int n, int i) { if (i > floor(sqrt(n))) return 1; if (n % i == 0) return 0; return isPrimeRecurs2(n, i + 2); } int main() { printf("Please enter a number "); int n; scanf("%i", &n); int result = isPrimeRecurs(n); printf("%s\n", result ? "prime" : "not prime"); }
Mihail
Now I understand what he was trying to explain
Anonymous
that is it
Anonymous
that is the entire code I have that he made
Anonymous
now I have to create a loop with recursion
Mihail
#include <stdio.h> #include <math.h> #include <stdlib.h> int isPrimeRecurs2(int n, int i); int isPrimeRecurs(int n) { if (n <= 1) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; return isPrimeRecurs2(n, 3); } int isPrimeRecurs2(int n, int i) { if (i > floor(sqrt(n))) return 1; if (n % i == 0) return 0; return isPrimeRecurs2(n, i + 2); } int main() { printf("Please enter a number "); int n; scanf("%i", &n); int result = isPrimeRecurs(n); printf("%s\n", result ? "prime" : "not prime"); }
#include <stdio.h> #include <math.h> #include <stdlib.h> int isPrimeRecurs2(int n, int i); int isPrimeRecurs(int n) { if (n <= 1) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; return isPrimeRecurs2(n, 3); } int isPrimeRecurs2(int n, int i) { if (i > floor(sqrt(n))) return 1; if (n % i == 0 && n != i) return 0; return isPrimeRecurs2(n, i + 2); } int main() { printf("Please enter a number "); int n; scanf("%i", &n); int result = isPrimeRecurs(n); printf("%s\n", result ? "prime" : "not prime"); }
Mihail
Try this
Anonymous
its not making loop :(
klimi
Mihail youre awesome
Anonymous
and i got to submitted in 4 hours
Mihail
I've got to go soon
Mihail
So can't help you anymore
Mihail
But there's lots of examples on this online
Anonymous
no loop
Mihail
Search "c prime number recursion"
Mihail
no loop
There's no loop there
Anonymous
the code is working very good
Anonymous
the thing I need is a loop
Anonymous
but without for, while and do while
Mihail
the thing I need is a loop
Well that's not exactly a loop
Mihail
But there's nothing else you could do to imitate a loop
Anonymous
how can i create a loop
Mihail
Mihail
Unless you want to use assembly
Anonymous
how is that
Mihail
It's the language that C compiles to
Mihail
There is no way to make your own loop in C
Mihail
(unless you want to use goto, but don't)
Mihail
Like you're trying to use a loop without using a loop
Mihail
Not sure what exactly you're trying to do
Anonymous
in the assignment he said "there is many ways to creat a loop without do while, while and for"
BinaryByter
There is no way to make your own loop in C
recursion, lobgjmp, inline assembly, goto, templated recursion, functor calling this->operator()
Anonymous
m Program Specifications: Write a C program that does not use a do while, while or for loop. The program will enter any number between 2 and INT_MAX from the user. The program will output if the number entered was PRIME or NOT PRIME. Example Run: Please enter a number between 2 and 2147483647: 7 The number 7 is a prime number. Example Run Please enter a number between 2 and 2147483647: 2 The number 2 is a prime number. Please enter a number between 2 and 2147483647: 77 The number 77 is NOT a prime number. RULES: You are not permitted to use the following commands:  while  do while  for  goto (by the way, you are NEVER ALLOWED TO USE goto)  There is another way to do a loop in C, Java, C++ and other programs
Mihail
The other's are just assembly
Mihail
Which I mentioned
Mihail
No
Mihail
Like don't even bring that up
Mihail
Please
BinaryByter
Anonymous
omg so what can i do?
BinaryByter
MACRO TEMPLATES
BinaryByter
templating in C
Anonymous
how is thaaat?
Mihail
omg so what can i do?
Just use recursion
Mihail
Unless they told you not to
Anonymous
but how can i add it? im trying but i do not know
Anonymous
Anonymous
but i do not know
Mihail
This is recursion
Anonymous
but it doesnt make a loop
Mihail
The fact that isPrimeRecurs2 keeps on calling itself until a condition is met is called recursion
Anonymous
but when it calls itself, it should be appearing again, right?
Anonymous
I mean, what is the fact that it calls itself and dont make a loop?
Anonymous
and y'all says that i have to use recursion for the loop
Anonymous
😭😭😭😭 i dont understand
Anonymous
im gonna kill myself
Anonymous
and so, why y'all says i have to use recursion?
Mihail
and so, why y'all says i have to use recursion?
Because it's the only way if you can't use a loop
Anonymous
so the program WILL NEVER give the user the option to put another number?