Sum of AP (n) = 1+2+…+n

8
Sum of AP (n) = 1+2+…+n sumofAP(4) = 4+3+2+1 sumofAP(3) = 3+2+1 sumofAP(2) = 2+1 sumofAP(1) = 1 sumofAP(0) = 0 int sumofAP (int n){ int k, sum=0; return sum; } n = sumofAP(4); printf ("sum = %i\n", n); int sumofAP (int n){ return } = +sumofAP( ) = +sumofAP( ) = +sumofAP( ) = +sumofAP( ) =0 if (n<=0) return else 1 遞遞 Recursion Recursion

description

遞歸 Recursion. Sum of AP (n) = 1+2+…+n. int sumofAP ( int n){ int k, sum=0; return sum; }. int sumofAP ( int n ){ return }. if (n

Transcript of Sum of AP (n) = 1+2+…+n

Page 1: Sum of AP (n) = 1+2+…+n

1

Sum of AP (n) = 1+2+…+n

sumofAP(4) = 4+3+2+1sumofAP(3) = 3+2+1sumofAP(2) = 2+1sumofAP(1) = 1sumofAP(0) = 0

int sumofAP (int n){int k, sum=0;

return sum;}

n = sumofAP(4);printf ("sum = %i\n", n);

int sumofAP (int n){

return}

= +sumofAP( )= +sumofAP( )= +sumofAP( )= +sumofAP( )=0

if (n<=0) returnelse

遞歸Recursion

Recursion

Page 2: Sum of AP (n) = 1+2+…+n

2

int fibonacci (int n){int i, a=1,b=1,c;if (n<=2) return (1);

return (c);}

1,1,2,3,5, 8,13,…

1,2,3,4,5, 6,7

a,b,c

a,b,c

每個數 c都是前兩數之和 (a+b)

fibonacci(n)= fibonacci( ) + fibonacci( )

Recursion

Page 3: Sum of AP (n) = 1+2+…+n

3

int fibonacci (int n){if ( )

return ( );else

return ( );}

1,1,2,3,5, 8,13,…

fib(4) + fib(3)

fib(3) + fib(2)

fib(5)

1,2,3,4,5, 6,7Recursion

Recursion

Page 4: Sum of AP (n) = 1+2+…+n

4

int gcd (int a, int b){int c=9;while (c>0){

}return (b);

}

a b c

234 % 144 = 90

144 % 90 = 54

90 % 54 = 36

54 % 36 = 18

36 % 18 = 0

a b c

int gcd (int a, int b){if ( )

return (b);else

return ( );}

HCF = Highest Common FactorGCD = Greatest Common Divisor

Recursion

234 144

Recursion

Page 5: Sum of AP (n) = 1+2+…+n

5

long factorial (int n){int i;long result=1;

return (result);}

long factorial (int n){if ( )

return 1;else

return ();}

(n!) Factorial 5! = 54321

Recursion

= 5 * factorial(4)= 5 * 4 * factorial(3)= 5 * 4 * 3 * factorial(2)= 5 * 4 * 3 * 2 * factorial(1)= 5 * 4 * 3 * 2 * 1

factorial(5)5

Recursion

Page 6: Sum of AP (n) = 1+2+…+n

6

double power (double x, int n){if (n==0)

returnelse if(n>0)

returnelse

return}

double power (double x, int n){int i, result=1;

return (result);}

Recursion

t = power(2,10); = 2222222222

1024210

55

21

2

2 10

Recursion

Page 7: Sum of AP (n) = 1+2+…+n

7

void toBin (int n){

}

void toHex (int n){char hexchar[] = "0123456789ABCDEF";

}

toBin(12) 1100toBin(65) 100 0001

toHex(58) 4A

Recursion

Page 8: Sum of AP (n) = 1+2+…+n

8

void toWord (int n){char *wd[ ] = {

"Zero", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine" };

}

void reverseChar(){char ch;

}

輸入reverse esrever

toWord(123) One Two Three

Recursion