You are on page 1of 3

5

: .1
( )T(n
(1)
n > 1 ,T(1) = 1
:( 0-
. T (n) = 2T ( n / 4 ) + 5
: .x x
. 5.1 = 6, 2.9 = 3, 12 .0 =12
: n=10

T (10) = 2T ( 10 / 4 ) + 5 = 2T ( 3 ) + 5 = 2( 2T ( 3 / 4 ) + 5) + 5 = 2( 2T (1) + 5) + 5 = 2(2 + 5) + 5 = 19

n
.(T(n
(int T(int n
:
,

.math.h ceil
.(T(n
(2)
:( , )

Please enter a positive integer:


10
T(10) = 19

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

/*the recursive function "T"*/


/*function T uses library function "ceil" from math.h library */
int T(int n)
{
int finalT=0;
double topnum=0;
if(n == 1)
return n;
else
{
/* function T must get an int defined variable (according to the
instruction)but function ceil can't get int variable */
/*functipn ceil must get and return double/long double/float variables
*/
topnum = ceil((double)n/4);
/* topnum variable must be defined as double in order to get the value
from function ceil but will always contain int value */
/*therefore topnum can be used as int without causing data lost*/
finalT =(2*(T((int)topnum))+5);
return finalT;
}
}
int main()
{
int num;
scanf_s("%d",&num);
printf("function T returne %d for T(%d) \n", T(num), num);
return 0;
}

.2 :

,
)(1
" , .
, 50 .5 5 2
.
.5 2 :

(void factor_rec(int n :
:
) (1-
.
.
, ,
, .
)(
.
) ,(:
Please enter a positive integer:
168
The prime factors of 168 are 2 3 7

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

//this function recieves an integer and prints it's prime factors.


)void factor_rec(int num
{
;int i=2, newnum=0
/*this loop will run until the smallest factor is found*/
)if (num==1
;)"printf("1
else
)while(2<=num
{
)if(num%i==0
{
;newnum=num/i
;)printf("%d ",i
/*the next loop divides the original number over i*/
/*the number will be divided time after time until it can't divide
again without remainder*/
)while(newnum%i==0
;newnum=newnum/i
/*continueing to the next factor over the new number*/
;)factor_rec(newnum
;break
}
/*if i is not a factor*/
else
;i++
}
}

)(int main
{
;int x
;)"printf("please enter a positive integer: \n
;)scanf_s("%d",&x
;)printf("the prime factors of %d are: \n",x

factor_rec(x);
return 0;
}

You might also like