Prime numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guiromero
    New Member
    • Nov 2021
    • 4

    Prime numbers

    I'm writing a program to calculate the number of prime numbers in a sequence. There's a variable called num that is given and then the program must calculate how many prime numbers there are from 2 (the smallest prime number) to num.

    It should be something like that:

    Enter a number greater than 1: 7 There are 4 prime numbers between 2 and 7.

    This is because 2,3,5 and 7 are prime numbers, a total of 4 numbers.

    The code below shows the variable num which is equal to 7, the main function calls isPrime (which says if a number is prime or not), then the way I thought about was using a for loop from 2 to num and check if each i is prime or not.

    I don't know how to proceed from that. Please, could someone help?

    #include <stdio.h>
    #include <math.h>

    int isPrime(int num) {
    int i;
    for (i=2; i<=sqrt(num); i++) {
    if (num%i == 0) {
    return 0;
    }
    }
    return 1;
    }


    int main(){

    int i, num, numofprimes;

    num=7;

    for (i=2;i<=num;i++ ){
    if (isPrime(i)){

    }

    printf("%d", numofprimes);
    }

    return 0;
    }
    If someone could help I would be glad.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    I'm writing a program to calculate the number of prime numbers in a sequence. There's a variable called num that is given and then the program must calculate how many prime numbers there are from 2 (the smallest prime number) to num.

    It should be something like that:

    Enter a number greater than 1: 7 There are 4 prime numbers between 2 and 7.

    This is because 2,3,5 and 7 are prime numbers, a total of 4 numbers.

    The code below shows the variable num which is equal to 7, the main function calls isPrime (which says if a number is prime or not), then the way I thought about was using a for loop from 2 to num and check if each i is prime or not.

    I don't know how to proceed from that. Please, could someone help?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int isPrime(int num) {
    int i;
    for (i=2; i<=sqrt(num); i++) {
    if (num%i == 0) {
    return 0;
    }
    }
    return 1;
    }
    
    
    int main(){
    
    int i, num, numofprimes;
    
    num=7;
    
    for (i=2;i<=num;i++){
    if (isPrime(i)){
    
    }
    
    printf("%d", numofprimes);
    }
    
    return 0;
    }
    If someone could help I would be glad.
    Code:
    int main() {
    
      int i, num, numofprimes = 0;
    
      num = 7;
    
      for (i = 2; i <= num; i++) {
        if (isPrime(i)) {
          numofprimes++;
        }
      }
      printf("%d", numofprimes);
    
      return 0;
    }

    Comment

    • guiromero
      New Member
      • Nov 2021
      • 4

      #3
      Thanks very much for your response.

      I'd like go further in this code.
      I 've created a function biggestPrime to tell the biggest prime number between 2 and num. The only way I could think about doing this was creating an array with numofprimes as its dimension, storing each prime number in the array and then identifying the biggest number in that array. However, I have no idea how to do that.

      #include <stdio.h>
      #include <math.h>

      int isPrime(int num) {
      int i;
      for (i=2; i<=sqrt(num); i++) {
      if (num%i == 0) {
      return 0;
      }
      }
      return 1;
      }


      int biggestPrime(in t num){

      int i, num, numofprimes;

      num=7;


      for (i=2;i<=num;i++ ){
      if (isPrime(i)){
      numofprimes++;
      }
      }

      int Arr [numofprimes] =

      return 0;
      }


      Does anyone know thow to do that?

      Thank you.

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        Thanks very much for your response.

        I'd like go further in this code.
        I 've created a function biggestPrime to tell the biggest prime number between 2 and num. The only way I could think about doing this was creating an array with numofprimes as its dimension, storing each prime number in the array and then identifying the biggest number in that array. However, I have no idea how to do that.

        Code:
        #include <stdio.h>
        #include <math.h>
        
        int isPrime(int num) {
        int i;
        for (i=2; i<=sqrt(num); i++) {
        if (num%i == 0) {
        return 0;
        }
        }
        return 1;
        }
        
        
        int biggestPrime(int num){
        
        int i, num, numofprimes;
        
        num=7;
        
        
        for (i=2;i<=num;i++){
        if (isPrime(i)){
        numofprimes++;
        }
        }
        
        int Arr [numofprimes] =
        
        return 0;
        }

        Does anyone know thow to do that?

        Thank you.
        Code:
        int biggestPrime(int num) {
          for (int i = num; i >= 2; i--)
            if (isPrime(i))
              return i;
        }

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Look of Sieve of Erastothenes. That may be a more efficient algorithm for finding all the prime numbers in a given range.

          Comment

          • james121
            New Member
            • Feb 2022
            • 1

            #6
            Also need this, got many points from comments. Thanks.

            Comment

            Working...