Check If A Number Is Prime | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 2024

ความคิดเห็น • 28

  • @lucgagnon7169
    @lucgagnon7169 7 หลายเดือนก่อน

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

  • @CoverDiaries
    @CoverDiaries 6 หลายเดือนก่อน

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

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

  • @King_Baldwin_IV_777
    @King_Baldwin_IV_777 3 หลายเดือนก่อน

    Thanks man !

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

  • @antoniomamic150
    @antoniomamic150 ปีที่แล้ว +1

    What a great tutorial! Thank you!

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

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

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

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

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

  • @jomez2841
    @jomez2841 ปีที่แล้ว

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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      Yes, we can optimize it even more that way. :-)

  • @flipzenpai3401
    @flipzenpai3401 ปีที่แล้ว

    Very understandable. Thank you

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

  • @walidoulondon8107
    @walidoulondon8107 ปีที่แล้ว

    Best teacher ever ❤

  • @xxz4655
    @xxz4655 2 ปีที่แล้ว

    Amazimg