C Programming Tutorial 73 - Check if Number is Prime (Counting Prime Numbers Part 2)

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 2024
  • Start your software dev career - calcur.tech/de... 💯 FREE Courses (100+ hours) - calcur.tech/al...
    🐍 Python Course - calcur.tech/py...
    ✅ Data Structures & Algorithms - calcur.tech/ds...
    ~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
    ✉️ Newsletter - calcur.tech/ne...
    📸 Instagram - / calebcurry
    🐦 Twitter - / calebcurry
    🔗 LinkedIn - / calebcurry
    ▶️ Subscribe - calcur.tech/sub...
    👨🏻‍🎓 Courses - www.codebreakt...
    ~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
    ↪ My Amazon Store - www.amazon.com...
    🅿 Patreon - calcur.tech/pat...
    🅖 GitHub Sponsors - github.com/spo...
    Ⓟ Paypal - paypal.me/calcur
    🅑 Bitcoin - 3HnF1SWTzo1dCU7RwFLhgk7SYiVfV37Pbq
    🅔 Eth - 0x350139af84b60d075a3a0379716040b63f6D3853
    📈 Buy Bitcoin - calcur.tech/cr...
    Reserve the Ruby Steel crypto rewards card and get a $25 bonus (use affiliate code "Caleb") - calcur.tech/cr...

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

  • @PunmasterSTP
    @PunmasterSTP 3 ปีที่แล้ว +5

    Check if a number is prime? More like watch these videos for a good time! Thanks again for making and uploading all this amazing content.

  • @adventdanielkarabo9558
    @adventdanielkarabo9558 3 ปีที่แล้ว +6

    i like how you not so tense on your videos appreciated maa`n

  • @kyleeames8229
    @kyleeames8229 4 ปีที่แล้ว +21

    Excellent tutorials. You only need to go up to n/2 to determine whether a number is prime. Moreover, if you store all the prime numbers identified up to n in an array, these can be tested up the largest before going on to consecutive integers larger than the largest prime as in the range 2 to the largest prime number identified up to n/2 at least one prime factor, if any factor will exist.

    • @cheerios623
      @cheerios623 4 ปีที่แล้ว +7

      You actually only need to check up to sqrt(n)!

    • @7s9n
      @7s9n 3 ปีที่แล้ว +1

      @@cheerios623 yeah 💛

    • @janlangeloh3269
      @janlangeloh3269 3 ปีที่แล้ว +4

      Yes because if it is not divisible by any number smaller than the square root of n there could be no one higher. Look if divide 21 by 3 you get the result 7. 7 is higher than sqrt(21), but still a divider. On the other hand side you alswase need to have a num smaller than sqrt(n) to allow this. Let's do 16 instead: 2 and 8 try to get closer: 4 and 4 there could not be a higher num because if there would the other pair had to be smaller than 4. 👍👌🖖

  • @UnusRamadan-db2vg
    @UnusRamadan-db2vg 2 หลายเดือนก่อน +1

    That's very helpful

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

    bro you love pizza more than anythingggg lolololo

  • @dantecastillodelasfuentes1026
    @dantecastillodelasfuentes1026 5 ปีที่แล้ว +4

    you´re the best

  • @filip6472
    @filip6472 2 ปีที่แล้ว +5

    As soon as you said Modulus and remainder is 0 for prime I knew how to code it.Thanks again for the tutorials they actually taught C better than other methods I tried.

  • @abhishekrbhat8919
    @abhishekrbhat8919 3 ปีที่แล้ว +2

    Thank you very much!

  • @josephinehannah103
    @josephinehannah103 3 ปีที่แล้ว +2

    Omg thank you so much!!

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

    It's good if it's small numbers but for greater numbers they're more efficient algorithm to find prime numbers

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

    bool isPrime(n) {
    for(let x=2; x

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

      This is why braces are the dumbest way to organize code.
      Here's the same code in Python.
      def isPrime(n):
      for x in range(2,int(sqrt(n)+1)):
      if not(n%x):
      return False
      return True

    • @-keios8170
      @-keios8170 11 หลายเดือนก่อน

      I'm clinging, but braces are not used to organize code. It is simply a grouping instruction, it collects several instructions into one, which can be then used at the same time, for example, in a conditional statement. For example in java:
      public static boolean isPrime(int n){
      if( n

  • @capnnemo5330
    @capnnemo5330 3 ปีที่แล้ว +4

    Hey everyone, I tried to code this little project myself but only the first number's check is correct can you help me with the problem?
    the ./a.out file is below:
    5 is a prime number
    4 is not a prime number
    3 is not a prime number
    2 is not a prime number
    1 is not a prime number
    #include
    int main()
    {
    int d=0;

    for(int i=4; i>0;i--)
    {
    for(int k=i;k>0; k--)
    {
    if( 0 == i % k)
    {
    d=d+1;
    }
    else if( 0 != i%k)
    {
    d=d;
    }
    }
    if(d==2)
    {
    printf("%i is a prime number
    ",i);

    }
    else if(d != 2)
    {
    printf("%i is not a prime number",i);

    }
    printf("

    ");
    }

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

      I think you need to reset d = 0 at the beginning of each iteration of the outer loop, like this:
      ...
      for(int i = 4; i > 0; i--)
      {
      d = 0;
      for(int k = i; k > 0; k--)
      {
      ...
      If you don't reset d, then it will just keep increasing for each number. Therefore it would never be equal to 2 again, and every number (except for possibly the first number) would seem not prime (i.e. "composite").

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

    Thanks, this video really made it simple to understand!

  • @semihazah0352
    @semihazah0352 ปีที่แล้ว +3

    Finally, a video that has helped me complete the prime number loop for my lab. So many videos left me lost and confused. This is so much simpler when talked about at the simplest level.

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

      Kardesim selaminaleykum, cs50'den bahsediyorsun sanirim. Nasil gidiyor bitirdin mi?

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

      currently doing one for my lab.

  • @Zen-lz1hc
    @Zen-lz1hc ปีที่แล้ว

    LIKE :)
    Great video thanks You!

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

    the best teacher

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

    The Best

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

    2?

  • @tv..6531
    @tv..6531 4 ปีที่แล้ว

    th-cam.com/video/0ABnO_WoexE/w-d-xo.html
    를 이용하여
    1부터 1,000까지 사이에 있는
    소수의 개수(counting prime numbers)를 구하는 영상 입니다.

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

    What if the input here is 2, which should be a primenumber. The program would mark it as not prime since 2%2=0.
    Is there a way of implementation without an additional if statement, that explicitly checks if the input is 2?

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

      yes

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

      There are lot ways to do it, man

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

      Check here
      th-cam.com/video/Eyl17WgH218/w-d-xo.html
      Subscribe if you like the code

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

      @@tiagodmota5840 but how me too if input is 2 then the output will look like 2 is not prime number

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

      @@izanyibukayvette381 There should be a way to do it mathematically, as we're not mathematicians use an if statement. I said that there's a way because computer scientists work a lot when the subject is prime numbers, research for an algorithm that is 100% solid mathematically

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

    You're such an amazing teacher

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

    -7 is a prime number?

    • @tiagodmota5840
      @tiagodmota5840 3 ปีที่แล้ว +2

      I googled and found this: "Prime numbers are a mathematical concept that describes POSITIVE whole numbers that can only be divided evenly by two other whole numbers (or factors)."

  • @paradise618
    @paradise618 3 ปีที่แล้ว

    input=1
    Result 1 is a prime number :(

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

      input should always be above 1 by default considering its already mathematically proven to not be prime, I understand concern tho, but result is due to 1 being only other number requiring diff algo to determine whether prime or not--confused me too

  • @adefolasayogboyega-adejuwo7285
    @adefolasayogboyega-adejuwo7285 3 ปีที่แล้ว

    please can you code it

  • @gamer-ox7tg
    @gamer-ox7tg 2 ปีที่แล้ว

    Thanks man

  • @jadoaesra3011
    @jadoaesra3011 3 ปีที่แล้ว

    I DID IT!!!!

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

    hi guys, what is wrong with my code?
    #include
    #include
    int main()
    {
    int input, i;
    printf("enter a number:");
    scanf("%d", &input);
    for(int i=2; i