Skip to main content
3 of 10
added 245 characters in body
Tal
  • 1.4k
  • 7
  • 13

#Perl, 120 111 ($x,$n)=<>=~/\S+/g;$s=0;$c=$t=1;for(1..$n){$t*=$x/$;($%2?$s:$c)+=$t*($_%4>1?-1:1)}printf("%.8f\n%.8f",$s,$c);

Ungolfed:

($x,$n)=<>=~/\S+/g;
$s=0;$c=$t=1;
for(1..$n){
  $t*=$x/$_;
  ($_%2? $s : $c) += $t*($_%4>1?-1:1)
}
printf("%.8f\n%.8f",$s,$c);

Basically loops from 1 to n, and updates $t to be equal x^$_/$_! ($_ is the loop index). It then decides whether to add it to the sine or cosine depending on whether $_ is odd or even, and decides its sign based on it's value modulo 4.

Tal
  • 1.4k
  • 7
  • 13