C function to determine if a number is a prime number

  • Write a C function that returns true if the input is a prime number and returns false otherwise:

    int isPrime(int n)
    {
        int isPr = 1;   // 1 = True
    
        for ( int i = 2; i < n; i++ )
        {
            if ( n%i == 0 )
            {
                isPr = 0;  // n is not prime
                break;
            }
        }
    
        return isPr;
    }
    
    

     

C function to determine if a number is a prime number

  • Write a C function that returns true if the input is a prime number and returns false otherwise:

    int isPrime(int n)
    {
        int isPr = 1;   // 1 = True
    
        for ( int i = 2; i < n; i++ )
        {
            if ( n%i == 0 )
            {
                isPr = 0;  // n is not prime
                break;
            }
        }
    
        return isPr;
    }
    
    

    The return type of the function is int (that's how C usually represent boolean values)

C function to determine if a number is a prime number

  • Write a C function that returns true if the input is a prime number and returns false otherwise:

    int isPrime(int n)
    {
        int isPr = 1;   // 1 = True
    
        for ( int i = 2; i < n; i++ )
        {
            if ( n%i == 0 )
            {
                isPr = 0;  // n is not prime
                break;
            }
        }
    
        return isPr;
    }
    
    

    We initially assumes that the input n is a prime number (and check this assumption next).

C function to determine if a number is a prime number

  • Write a C function that returns true if the input is a prime number and returns false otherwise:

    int isPrime(int n)
    {
        int isPr = 1;   // 1 = True
    
        for ( int i = 2; i < n; i++ )
        {
            if ( n%i == 0 )
            {
                isPr = 0;  // n is not prime
                break;
            }
        }
    
        return isPr;
    }
    
    

    We check if n is prime by divide it by every possible divisor...
     

C function to determine if a number is a prime number

  • Write a C function that returns true if the input is a prime number and returns false otherwise:

    int isPrime(int n)
    {
        int isPr = 1;   // 1 = True
    
        for ( int i = 2; i < n; i++ )
        {
            if ( n%i == 0 )
            {
                isPr = 0;  // n is not prime
                break;
            }
        }
    
        return isPr;
    }
    
    

    If we find a divisor for n, then it is not prime and we can stop
     

C function to determine if a number is a prime number

  • Write a C function that returns true if the input is a prime number and returns false otherwise:

    int isPrime(int n)
    {
        int isPr = 1;   // 1 = True
    
        for ( int i = 2; i < n; i++ )
        {
            if ( n%i == 0 )
            {
                isPr = 0;  // n is not prime
                break;
            }
        }
    
        return isPr;
    }
    
    

    When the loop finishes, we return the result
     

DEMO: demo/C/set1/c-prog1.c

Factoring an integer number

  • Every integer number can be factored into its prime number factors

  • The prime number factors of a number x are prime numbers whose product is equal to the number x

  • Example:

     The prime factors of  6 are: 2  3       (2 × 3 = 6)
       
     The prime factors of 12 are: 2  2  3    (2 × 2 × 3 = 12)
    
     The prime factors of 90 are: 2  3  3  5 (2 × 3 × 3 × 5 = 180)
      

  • Write a C program that reads in an integer number and prints out its prime factors

Factoring an integer number

  • Develop a plan:

       How do you factor the number 60 ?:
      
           60 = ???
      
      
      
      
      
      
      
      
      
      
      
      
      
        

Factoring an integer number

  • Develop a plan:

       How do you factor the number 60 ?:
      
           60 = ???
      
       (1) Try dividing by 2: 60%2 == 0  --> 2 is a factor
           60/2 = 30  ===> now factor 30 
           
       (2) Try dividing by 2: 30%2 == 0  --> 2 is a factor
           30/2 = 15  ===> now factor 15
      
       (3) Try dividing by 2: 15%2 == 1  --> no more 2's, try next factor
      
       (4) Try dividing by 3: 15%3 == 0  --> 3 is a factor
           15/3 = 5   ===> now factor 5 
      
       (5) Try dividing by 3:  5%3 == 2  --> no more 3's, try next factor
      
       (6) Try dividing by 4:  5%4 == 1  --> no more 4's, try next factor
      
       (7) Try dividing by 5:  5%5 == 0  --> 5 is a factor
           5/5 = 1  ===> 1 cannot be factored -- STOP  

Factoring an integer number

  • Write out the steps of the plan:

       Read in number from input;
      
       nextFactor = 2;
      
       while ( number != 1 )
       {
          if ( number is divisible by nextFactor )
          {
              print nextFactor;
              reduce number to   number/nextFactor
          }
          else
          {
              try nextFactor+1 as nextFactor
          }
       }
       

Factoring an integer number

  • Translate the steps of the plan to C code:

      #include <stdio.h>
      
      int main(int argc, char *argv[])
      {
      
          int x;
          int nextFactor;
             
          printf("Enter a number: ");
          scanf("%d", &x);
      
          nextFactor = 2; // Initialize nextFactor
      
          while ( x != 1 )
          {
              if ( x%nextFactor == 0 )
              {
                  printf("%d\n", nextFactor); // Pront new factor
                  x = x / nextFactor;         // Remove factor from number
              }
              else
                  nextFactor = nextFactor+1;  // Try next factor
          }
      }

DEMO: demo/C/set1/c-prog2.c