/* Computes the Taylor Series for e^x for n terms */
#include<stdio.h>
float powerR( float x, int n) {
if ( n == 0 ) return 1.0 ;
return x * powerR( x, n- 1 ) ;
}
int factR( int n) {
if ( n == 1 ) return 1 ;;
return n * factR( n- 1 ) ;
}
float TaylorR( int x, int n) {
if ( n == 0 ) return 1.0 ; // Since the series starts with 1
return TaylorR( x, n- 1 ) + ( powerR( x, n) / factR( n) ) ;
}
float Taylor( int x, int n) {
int i;
float sum = 0.0 ;
printf ( "Talor Series with x=%d and n=%d: " , x
, n
) ; for ( i= 1 ; i< n; i++ )
sum = sum + ( powerR( x, i) / factR( i) ) ;
return sum = sum + 1 ; //Since series starts with 1
}
int main( ) {
int x, n, i;
printf ( "Give the values of x and n (number of terms):" ) ; for ( i
= 1 ; i
<= n
; i
++ ) printf ( "%f\n " , Taylor
( x
, i
) ) ; for ( i
= 1 ; i
<= n
; i
++ ) printf ( "%f\n " , TaylorR
( x
, i
- 1 ) ) ; }
LyogQ29tcHV0ZXMgdGhlIFRheWxvciBTZXJpZXMgZm9yIGVeeCBmb3IgbiB0ZXJtcyAqLwojaW5jbHVkZTxzdGRpby5oPgkKZmxvYXQgcG93ZXJSKGZsb2F0IHgsIGludCBuKXsKCWlmIChuID09IDApIHJldHVybiAxLjA7CglyZXR1cm4geCAqIHBvd2VyUih4LG4tMSk7Cn0KaW50IGZhY3RSKGludCBuKXsKCWlmIChuID09IDEpIHJldHVybiAxOzsKCXJldHVybiBuICogZmFjdFIobi0xKTsKfQpmbG9hdCBUYXlsb3JSKGludCB4LCBpbnQgbil7CglpZiAobiA9PSAwKSByZXR1cm4gMS4wOyAvLyBTaW5jZSB0aGUgc2VyaWVzIHN0YXJ0cyB3aXRoIDEKICAgIHJldHVybiBUYXlsb3JSKHgsbi0xKSArIChwb3dlclIoeCxuKS9mYWN0UihuKSk7Cn0KZmxvYXQgVGF5bG9yKCBpbnQgeCwgaW50IG4pewogICAgaW50IGk7CglmbG9hdCBzdW0gPSAwLjA7CglwcmludGYo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