Mat
Let's say recursion is like Induction
Anonymous
😭😭😭 im getting crazy
Anonymous
😭😭😭 im getting crazy
https://www.tutorialspoint.com/cprogramming/c_recursion.htm
RAHUL
Hello everybody
RAHUL
Please solve my problem
Anonymous
https://www.tutorialspoint.com/cprogramming/c_recursion.htm
You don't have for beginners Shelly ?
RAHUL
RAHUL
Anonymous
You don't have for beginners Shelly ?
This is pretty easy and for beginners
Anonymous
You can't explain recursion even more easy
RAHUL
Hey please solve my problem
MᏫᎻᎯᎷᎷᎬᎠ
Tutorialspoint is a good start
Anonymous
im taking a look
Anonymous
thanks guys
Anonymous
Anonymous
Nice
I want a simple book I 've a book with 400 pages
Anonymous
an user suppose to enter a number and then, the program will make the decision if it is prime or not and loop it
Anonymous
and wow your code is good
Mihail
only one thing i'd change is have the "init" fuction be just isPrime and have the other be isPrimeRecurs or something, as it'll both be more accurate and more understandable
Anonymous
taking a look, it has the for loop and i cant
Mihail
he wrote that to show the algorithm
Mihail
check the second snippet
Anonymous
oh ya ya
Anonymous
wow
Anonymous
but my question is, where i can put the option for the user
Anonymous
check the second snippet
why is the isPrimeRecurs(107)
Mihail
why is the isPrimeRecurs(107)
he calls the function and that returns true or false, depending on if the number is prime
Mihail
he added 107 as an example
Mihail
that should be the number you want to check
Anonymous
i should let the user enter any number they want
Anonymous
Hii
Mihail
i should let the user enter any number they want
well just store the input on a variable and pass that to the function
Anonymous
well just store the input on a variable and pass that to the function
and the variable should be outside the main, right?
Anonymous
and the scanf have to be isPrimeRecurs?
Mihail
just use scanf to store the input in a variable
Mihail
then replace 107 with that variable
Anonymous
ok, what i know is that, if I introduce a number, it has to be scanned to store it in the varaible and then, a printf to show it
Mihail
yes
Mihail
in the printf use the result of isPrimeRecurs
Mihail
what don't you understand exactly?
Anonymous
ok let me show you
Anonymous
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 > sqrt(n)) return 1; if(n % i == 0) return 0; return isPrimeRecurs2(n, i + 2); } and call int main() { printf("Please enter a number"); scanf("%i", isPrimeRecurs); printf("%i \n", isPrimeRecurs); }
Anonymous
that should be the entire program?
Anonymous
take a look at the main
Mihail
store it in a variable
Anonymous
#ot
Mihail
you should make your functions return bools tho
Mihail
oh
Mihail
true
Anonymous
oooh got it!!!!!!!! omg im so sorry, i was very confused
Anonymous
thanks!
Anonymous
7 is a primer number, right?
BinaryByter
y
Anonymous
the code says it's not
Anonymous
BinaryByter
do you try dividing by 1?
Mihail
not sure exactly what you're trying to prove by that tbh
Mihail
an object that can store 0 or 1 is exactly what you're looking for
Anonymous
Good night all of you
Anonymous
Anonymous
guys
Anonymous
literally I do not know how to make the loop
Anonymous
@mmihov you there?
Mihail
Yes
Mihail
And don't PM me
Anonymous
vehlwn was helping me a lot
Anonymous
but he got to go
Anonymous
and literally the program is done except for the recursion
Anonymous
and he texted me this
Anonymous
every loop like: for(int i = 0; i <= k; i += 2) { if(check(i)) return; } can be rewritten with tail recursion functions like: callLoop() { // pass initial value for loop counter callLoop2(0); } callLoop2(int i) { // check stop conditions if(!(i <= k)) reutrn; if(check(i)) reutrn; // simulate loop iteration and increment loop counter callLoop2(i + 2); } in you task you need more conditions in 1st function and pass n as additional parameter: isPrimeRecurs(int n) { if(n <= 1) return 0; if(n == 2) return 1; if(n % 2 == 0) return 0; callLoop2(n, 3); } 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); }
Anonymous
but I do not understand how to apply that callLoop into the code