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

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

КОМЕНТАРІ • 44

  • @adventdanielkarabo9558
    @adventdanielkarabo9558 4 роки тому +7

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

  • @kyleeames8229
    @kyleeames8229 5 років тому +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 4 роки тому +1

      @@cheerios623 yeah 💛

    • @janlangeloh3269
      @janlangeloh3269 3 роки тому +5

      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. 👍👌🖖

  • @filip6472
    @filip6472 3 роки тому +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.

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

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

  • @PunmasterSTP
    @PunmasterSTP 3 роки тому +4

    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.

  • @UnusRamadan-db2vg
    @UnusRamadan-db2vg 4 місяці тому +1

    That's very helpful

  • @semihazah0352
    @semihazah0352 2 роки тому +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.

  • @abhishekrbhat8919
    @abhishekrbhat8919 3 роки тому +2

    Thank you very much!

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

    Thanks, this video really made it simple to understand!

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

    You're such an amazing teacher

  • @josephinehannah103
    @josephinehannah103 3 роки тому +2

    Omg thank you so much!!

  • @dantecastillodelasfuentes1026
    @dantecastillodelasfuentes1026 5 років тому +4

    you´re the best

  • @gamingwithjoshua1261
    @gamingwithjoshua1261 3 роки тому +1

    the best teacher

  • @gamer-ox7tg
    @gamer-ox7tg 2 роки тому

    Thanks man

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

    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 Рік тому

      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

  • @amishisharma7100
    @amishisharma7100 3 роки тому +1

    bro you love pizza more than anythingggg lolololo

  • @tv..6531
    @tv..6531 4 роки тому

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

  • @ilovecode2256
    @ilovecode2256 5 років тому +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 4 роки тому +1

      There are lot ways to do it, man

    • @allrounderzcreations
      @allrounderzcreations 4 роки тому +1

      Check here
      ua-cam.com/video/Eyl17WgH218/v-deo.html
      Subscribe if you like the code

    • @izanyibukayvette381
      @izanyibukayvette381 4 роки тому +1

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

    • @tiagodmota5840
      @tiagodmota5840 4 роки тому +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

  • @londonsystem6622
    @londonsystem6622 4 роки тому +1

    The Best

  • @capnnemo5330
    @capnnemo5330 4 роки тому +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").

  • @jadoaesra3011
    @jadoaesra3011 3 роки тому

    I DID IT!!!!

  • @tiagodmota5840
    @tiagodmota5840 4 роки тому +1

    -7 is a prime number?

    • @tiagodmota5840
      @tiagodmota5840 4 роки тому +1

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

  • @Zen-lz1hc
    @Zen-lz1hc 2 роки тому

    LIKE :)
    Great video thanks You!

  • @adefolasayogboyega-adejuwo7285
    @adefolasayogboyega-adejuwo7285 3 роки тому

    please can you code it

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

    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

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

    2?

  • @paradise618
    @paradise618 3 роки тому

    input=1
    Result 1 is a prime number :(

    • @chasemedsker
      @chasemedsker 3 роки тому

      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