COMP 1917 Computing 1
Session 2, 2016

Lab Solutions - Week 3


Note: This code should be appropriately commented.

  1. One possibility is to re-compute the sum each time:
    #include <stdio.h>
    
    int main( void )
    {
      int sum;
      int j,k;
    
      for( k=1; k <= 15; k++ ) {
        // re-compute sum from the beginning each time
        sum = 0;
        for( j=1; j <= k; j++ ) {
          sum += j;
        }
        printf(" %2d %4d\n", k, sum );
      }
    
      return 0;
    }
    
    However, the task can be accomplished more efficiently by retaining the sum from the previous iteration and just adding the latest number:
    #include <stdio.h>
    
    int main( void )
    {
      int sum;
      int k;
    
      sum = 0;
      for( k=1; k <= 15; k++ ) {
        // add the latest number to the previous sum
        sum += k;
        printf(" %2d %4d\n", k, sum );
      }
    
      return 0;
    }
    
  2. Change the looping part of the code to this:
      sum = 0;
      for( k=1; k <= 15; k++ ) {
        sum += k*k*k;
        printf(" %2d %5d\n", k, sum );
      }
    
    The sum of the cubes from 1 to k is equal to the square of the sum from 1 to k.
  3. #include <stdio.h>
    
    int main( void )
    {
      int sum=0;
      int k,n;
    
      printf("Enter number: ");
      scanf("%d",&n);
    
      printf("The factors of %d are:\n",n);
    
      for( k=1; k < n; k++ ) {
        if( n % k == 0 ) {
          printf("%d\n", k );
          sum += k;
        }
      }
      printf("%d\n", n ); // n is a factor, but not included in sum
    
      printf("Sum of factors = %d\n", sum );
    
      if( sum == n ) {
        printf("%d is a perfect number.\n", n );
      }
      else {
        printf("%d is not a perfect number.\n", n );
      }
    
      return 0;
    }
    
  4. Bonus Challenge
    #include <stdio.h>
    
    int main( void )
    {
       int number;
       int k,n;
    
       printf("Enter number: ");
       scanf("%d", &number);
       /*
          To find all prime factors of a number, we simply go through
          all numbers from 2 to the original number and see if the
          original number is divisible by any of them.
          We also need to take into account that there may be
          multiple prime factors of the
          same value, e.g. 12 = 2*2 * 3, so we have to keep dividing
          a number by the same factor until it is no longer divisible.
       */
       k = 2;
       n = number;
       while( n > 1 && k < number ) {
          if( n % k == 0 ) {
             if( n == number ) {
                printf("%d = ", number );
             } else {
                printf(" * ");
             }
             printf("%d",k);
             n = n / k;
             while( n % k == 0 ) {
                printf("*%d",k);
                n = n / k;
             }
          }
          k++;
       }
       if( n == number ) {
          printf("%d is prime", number);
       }
       printf("\n");
    
       return 0;
    }