Skip to main content
Commonmark migration
Source Link

#Perl, 120 108 104 89 85 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%f\n"x2,$s,$c

Perl, 120 108 104 89 85

<>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$_;$_%2?$s:$c+=$_&2?-$t:$t}printf"%f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

#Perl, 120 108 104 89 85 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

Perl, 120 108 104 89 85

<>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$_;$_%2?$s:$c+=$_&2?-$t:$t}printf"%f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

deleted 2 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13

#Perl, 120 108 104 89 8785 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%.6f\n"x2printf"%f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

#Perl, 120 108 104 89 87 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%.6f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

#Perl, 120 108 104 89 85 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

added 600 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13

#Perl, 120 108 104 89 87 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%.8f\n"x26f\n"x2,$s,$c

Ungolfed:

<>=~<> =~ / /;
$c=$t=1;$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t*=$`$t *= $` / $_;
  ($_%2 ? $s$sine : $c$cosine) += $_&2?-$t:$t
}
printf"%printf "%.8f\n"x26f\n" x2,$s $sine,$c $cosine

Basically loopsThe first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to n2*n-1, and updates. $t to be equalis our term, which the loop repeatedly multiplies by x^$_/$_!x and divides by the loop's index ($_ is the loop index). It then decides whetherThe loop starts at 1 rather than 0 because the cosine is initialized to add it1, which saved me having to deal with dividing by zero.

After updating $t, the sinetrinary operator returns either $sine or cosine depending on whether $_$cosine, depending on whether the index is odd or even. Whether each term needs, and adds $t's value to be added or subtracted is decided by theit. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (that's 1 minus the index-bitwisebasically using a bitwise-and-2 on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

#Perl, 120 108 104 89 87 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%.8f\n"x2,$s,$c

Ungolfed:

<>=~/ /;
$c=$t=1;
for(1..2*$'-1){
  $t*=$`/$_;
  $_%2? $s : $c += $_&2?-$t:$t
}
printf"%.8f\n"x2,$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. Whether each term needs to be added or subtracted is decided by the magic formula $_&2?-$t:$t (that's 1 minus the index-bitwise-and-2).

#Perl, 120 108 104 89 87 <>=~/ /;$c=$t=1;for(1..2*$'-1){$t*=$`/$;$%2?$s:$c+=$_&2?-$t:$t}printf"%.6f\n"x2,$s,$c

Ungolfed:

<> =~ / /;
$cosine = $t = 1;
for (1.. 2*$' - 1){
  $t *= $` / $_;
  ($_%2 ? $sine : $cosine) += $_&2?-$t:$t
}
printf "%.6f\n" x2, $sine, $cosine

The first line reads the input and uses regex to find a space; this automatically puts the value before the space in $` and the value after it in $'.

Now we loop from 1 to 2*n-1. $t is our term, which the loop repeatedly multiplies by x and divides by the loop's index ($_). The loop starts at 1 rather than 0 because the cosine is initialized to 1, which saved me having to deal with dividing by zero.

After updating $t, the trinary operator returns either $sine or $cosine, depending on whether the index is odd or even, and adds $t's value to it. The magic formula $_&2?-$t:$t figures whether to add or subtract this value (basically using a bitwise-and on the index and 2 to generate the repeating sequence of "add, add, subtract, subtract").

You can test-run this code at compileonline.com.

added 7 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading
deleted 19 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading
added 87 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading
deleted 3 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading
added 245 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading
deleted 163 characters in body
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading
Source Link
Tal
  • 1.4k
  • 7
  • 13
Loading