And there we go! A long-awaited correct version of the previous program is here. Thank you for remembering my suggestion in previous video about prime numbers!
Also I have one question: what is that IDE you are using? Sometimes I can see in your videos, you are using that kind of IDE, and other times I can see you are using some kind of text ediotor and compiling in the terminal. What is that text editor?
@@Klusio19 The text editor is Visual Studio Code. I've turned off a lot of features so they take up less screen space. The IDE I sometimes use is Xcode, where again I've turned off a lot of options to make it look that way.
duck debugger the AI designed by the staff of CS50 suggested to count from 2 to the square root of the number to improve the code speed I guess what do you think
Thank you for the details on gitub, very useful to re-work on it!!! I have a question for you sir: can you suggest something for the beginners about testing our program because something we run the program for two or tree values and don't know that we need to test anotherone just to see if the algorithm is enough correct? something like to anticipate the punsh as in the kung fu lol ... J'adore les petits details dans vos explications !!!😉 Thanks !
That's a really good question. :-) When testing out code, it's often a good idea to use "special cases" as inputs. There are different names for these special cases such as "boundary cases". But basically the idea is that if we have a program that is expected to change behaviour at a certain input, like x = 0, then good test cases would be x=-1, x=0, and x=1 because we are testing "the important part" where behaviour should change. Using test inputs like x=2, x=3, x=4 won't matter as much then testing "where the behaviour is expected to change". Maybe one day I can make a full video on this. :-)
Amazing video, very easy to understand! I have one question though...I am a little confused about how the computer process the FOR loop. The FOR loop still executes the body even if the boolean expression is false? for (int i = 2; i < number; i++) if (number % 2 == 0) return false; in this case, if "number" was equal 2 then the loop should stop, right? Because "i" is not smaller than "number" but equal.
Yes it's possible: www.sanfoundry.com/c-program-prime-number-using-recursion/. It won't really be a "pretty" solution in some sense, because you're essentially using a function parameter as a counter variable. We can translate any loop with a counter variable to a recursive function in this way, so that's why I say it's not "pretty" or "elegant", it's just sort of a "crude translation". But it will work. :-) Maybe I'll do a video on it, and a video on translating from a loop to recursion in general...
Great explanation, I'm so dumb I was waiting u the whole video to add 2 as special case but then I realized that if the loop didn't run, the function will return true anyway : bool is_prime(int number){ if(number < 1) {printf("Error,number is not natural"); return false;} if(number == 1) return false; if(number == 2) return true; // not necessary for(int i = 2 ; i
And there we go! A long-awaited correct version of the previous program is here. Thank you for remembering my suggestion in previous video about prime numbers!
Also I have one question: what is that IDE you are using? Sometimes I can see in your videos, you are using that kind of IDE, and other times I can see you are using some kind of text ediotor and compiling in the terminal. What is that text editor?
@@Klusio19 The text editor is Visual Studio Code. I've turned off a lot of features so they take up less screen space. The IDE I sometimes use is Xcode, where again I've turned off a lot of options to make it look that way.
What a great tutorial! Thank you!
You’re welcome Antonio, I’m glad you enjoyed it! :-)
#include
bool is_prime( int number )
{
if ( number
duck debugger the AI designed by the staff of CS50 suggested to count from 2 to the square root of the number to improve the code speed I guess what do you think
Thank you for the details on gitub, very useful to re-work on it!!!
I have a question for you sir: can you suggest something for the beginners about testing our program because something we run the program for two or tree values and don't know that we need to test anotherone just to see if the algorithm is enough correct? something like to anticipate the punsh as in the kung fu lol ... J'adore les petits details dans vos explications !!!😉 Thanks !
That's a really good question. :-) When testing out code, it's often a good idea to use "special cases" as inputs. There are different names for these special cases such as "boundary cases". But basically the idea is that if we have a program that is expected to change behaviour at a certain input, like x = 0, then good test cases would be x=-1, x=0, and x=1 because we are testing "the important part" where behaviour should change. Using test inputs like x=2, x=3, x=4 won't matter as much then testing "where the behaviour is expected to change". Maybe one day I can make a full video on this. :-)
Amazing video, very easy to understand!
I have one question though...I am a little confused about how the computer process the FOR loop.
The FOR loop still executes the body even if the boolean expression is false?
for (int i = 2; i < number; i++)
if (number % 2 == 0) return false;
in this case, if "number" was equal 2 then the loop should stop, right? Because "i" is not smaller than "number" but equal.
If the boolean expression (condition) is false, the for loop body will not execute.
A question sir, if instead of bool is_prime , bool check_prime is used?
Best teacher ever ❤
Awww, thank you! :-)
Is there a way to use recursion? Nice work!
Yes it's possible: www.sanfoundry.com/c-program-prime-number-using-recursion/. It won't really be a "pretty" solution in some sense, because you're essentially using a function parameter as a counter variable. We can translate any loop with a counter variable to a recursive function in this way, so that's why I say it's not "pretty" or "elegant", it's just sort of a "crude translation". But it will work. :-) Maybe I'll do a video on it, and a video on translating from a loop to recursion in general...
Thanks man !
Very understandable. Thank you
You’re welcome Flip! :-)
Great explanation, I'm so dumb I was waiting u the whole video to add 2 as special case but then I realized that if the loop didn't run, the function will return true anyway :
bool is_prime(int number){
if(number < 1) {printf("Error,number is not natural"); return false;}
if(number == 1) return false;
if(number == 2) return true; // not necessary
for(int i = 2 ; i
Also with the optimized version the loop does not run for the number : 3 🙃.
That's exactly it, the special case takes care of itself. :-)
Don’t you only need to check up until the square root of n?
Yes, we can optimize it even more that way. :-)
Amazimg
It's art
@@xxz4655 Thank you! :-D