Check If A Number Is Prime | C Programming Example

Поділитися
Вставка
  • Опубліковано 13 гру 2024

КОМЕНТАРІ • 28

  • @Klusio19
    @Klusio19 2 роки тому

    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!

    • @Klusio19
      @Klusio19 2 роки тому

      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?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      @@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.

  • @antoniomamic150
    @antoniomamic150 Рік тому +1

    What a great tutorial! Thank you!

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      You’re welcome Antonio, I’m glad you enjoyed it! :-)

  • @lucgagnon7169
    @lucgagnon7169 9 місяців тому

    #include
    bool is_prime( int number )
    {
    if ( number

  • @ahmedabdulgawad4758
    @ahmedabdulgawad4758 Рік тому

    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

  • @zoquevil4792
    @zoquevil4792 2 роки тому +1

    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 !

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      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. :-)

  • @UpsideDownSushi
    @UpsideDownSushi Рік тому

    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.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      If the boolean expression (condition) is false, the for loop body will not execute.

  • @CoverDiaries
    @CoverDiaries 9 місяців тому

    A question sir, if instead of bool is_prime , bool check_prime is used?

  • @walidoulondon8107
    @walidoulondon8107 Рік тому

    Best teacher ever ❤

  • @cryptotechcoder
    @cryptotechcoder Рік тому

    Is there a way to use recursion? Nice work!

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      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...

  • @King_Baldwin_IV_777
    @King_Baldwin_IV_777 6 місяців тому

    Thanks man !

  • @flipzenpai3401
    @flipzenpai3401 2 роки тому

    Very understandable. Thank you

  • @justcurious1940
    @justcurious1940 Рік тому

    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

    • @justcurious1940
      @justcurious1940 Рік тому

      Also with the optimized version the loop does not run for the number : 3 🙃.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      That's exactly it, the special case takes care of itself. :-)

  • @jomez2841
    @jomez2841 Рік тому

    Don’t you only need to check up until the square root of n?

  • @xxz4655
    @xxz4655 2 роки тому

    Amazimg