Docstoc

CSCI-455

Document Sample
CSCI-455 Powered By Docstoc
					Programming Training


Main Points:
      - Problems with Numbers.
      - Discuss some important algorithms
              - Primality
              - Digits
              - Greatest Common Divisor
C Repetitions.
 C repetitions execute repeatealy a statement while a
     condition holds or for a number of times.
 While loop
     while(test)statement;
     while(test){statement1;statement2; …}
 repeats statements zero or more times while test holds.
 Do while loop
     do{statement1;statement2; …}while(test);
 repeats statements once or more times while test
     holds.
For loop
 for(exp1;exp2;exp3)statement;
 for(exp1;exp2;exp3){statement1;statemet2;…}
      exp1 – initialise the loop index
      exp2 – control the loop index
      exp3 – modifies the loop index.
 Foor loop is used when we know some patterns in repetition.
    for(i=0;i<n;i++)stat; repeteat stat for i=0,1,2,…,n-1.
    for(i=n-1;i>=0;i--)stat; repeteat stat for i=n-1,n-2,..,0.
    for(;;)stat; repeat stat forever.
Find pow so that d^pow | n.
Counting problems use a counting counter cont:
    - initial value is 0.
    - count increases when the event occurs.
Any time when a divisor is found we remove it from the number.

Example: n=72 and k=2.
pow: 0 | 1 | 2 | 3
n : 72 | 36 | 18 | 9
d :2 |2 |2 |
The repetition takes place while n is divisible by d: n % d ==0
#include<stdio.h>
#include<conio.h>

int main (int args,char ** argc){
        long n, d, pow=0;

        printf(”n=");scanf("%ld",&n);
        printf(”d=");scanf("%ld",&d);
        while(n % d==0)
        {
            n = n / d;
            pow ++;
        }

        printf("\n The power is %ld”, pow);

return 1;
}
Prime number decomposition
If a number n is integer find the prime number decomposition.
Repeat for d=2,3,4,5,…?
      - if d is a divisor of n then
                  - find the power of d in n.
                  - write d and pow
Example: n=72
      n : 72 | 9 | 1 |
      d : 2 | 3 | 4 |
      pow : 3 | 2 |
Repetition ends when n<d.
#include<stdio.h>
#include<conio.h>

int main (int args,char ** argc){
          long n, d, pow=0;

            printf(”n=");scanf("%ld",&n);
            for(d=2;d<=n;d++){
                 if (n%d==0)
                 {
                     pow=0;
                      while(n % d==0)
                      {
                             n = n / d; pow ++;
                       }
                      printf(“ %ld ^ %ld * ”, d, pow);
               }

return 1;
}
Prime numbers

If a number n is integer then find whether is prime
     or not.
n is prime when n has only 2 divisors 1 and n.
Example:
     - n =2, 3, 5, 7 are all prime
     - even numbers are not prime.
     - 12345678910987654321 is prime
     -1234567891010987654321 is prime too
Prime numbers (1)

Some Remarks:
  n==2        n is prime.
  Even numbers: n%2==0  n is not prime
  Odd numbers have only odd divisors d=3,5,7,…
  Repeat for d=3,5,7,…sqrt(n)
     - if d is divisor then n is not prime.
#include<stdio.h>
#include<conio.h>

int main (int args,char ** argc){
          long n, d;
          printf(”n=");scanf("%ld",&n);
          if (n==2)
          {
                printf(“%d is prime \n”,n); return1;
          }
          else
          if (n%2==0)
          {
                printf(“%d is NOT prime \n”,n); return1;
          }
          for(d=2;d<=sqrt(n);d+=2){
                if (n%d==0)
                  {
                     printf(“%d is NOT prime \n”,n); return1;
                   }
          }

            printf(“%d is prime \n”,n); return1;

return 1;
}
Greatest Common Divisor

If a, b are integer then the greatest common divisor
Example:
     - a=24, b=36  gcd=12
     - a=25, b=33  gcd=1
How to solve it?
     - the Euclidian algorithm
The Euclidean Algorithm

Sequence of divisions until the remainder is 0.
Repeat the following:
   - divide the numbers
   - if remainder is 0 then break
   - change the numbers:
           first=second; second=remainder
#include<stdio.h>
#include<conio.h>

int main (int args,char ** argc){
          long a, b, first, last, remainder;

            printf(”a=");scanf("%ld",&a);
            printf(”b=");scanf("%ld",&b);

            do
            {  // divide first to second
               result=first / second; remainder=first % second;
               // change values for first and second
               first=second; second=remainder;

            }
            while(remainder!=0);
            gcd=second;
            printf("\n gcd(%d,%d)=%d”, a,b,gcd);

return 1;
}
Find the digits of an integer.
  If n is a long number then
        last=n % 10 is the last digit.
        n = n/10 is the new number from which we remove last.
  If we repeat removing the last digit then we can find:
        - all the digits of the number.
        - number of digits.
  Example: n=45672941
  last: 1           |4        |9     |2   |…
  n: 4567294 | 456729 | 45672 | 4567 | …

  We repeat while the number is not fully done.
#include<stdio.h>
#include<conio.h>

int main (int args,char ** argc){
          long n, last, count=0;

            printf(”n=");scanf("%ld",&n);

            while(n!=0)
            {
                 last = n % 10; printf(" %ld \n”, last);
                 n = n / 10;
                 cont ++;
            }

            printf("\n The numbers of digits is %ld”, count);

return 1;
}
To do List
 1.   Read more about while and for from the e-tutorial.
 2.   Solve the HW problems.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:2/26/2012
language:
pages:16