Skip to main content
updated to follow the new requirements
Source Link

CJam - 42 ASCII / 32 Unicode

Try either version online at http://cjam.aditsu.net

First, the ASCII version:

rd:X;1_ri2*,1>{_2%2*(*/X*_}/;]2/z{:+8mO+6mO}/p

Try it online at http://cjam.aditsu.net

Explanation:

  • 2* multiplies by 2
  • , makes an array from 0 to (last value)-1
  • 1> removes the first item of the array (0)
  • {...}/ executes the block for each item in the array
    _ duplicates the "loop variable" (let's call it k)
    2%2*( converts from even/odd to -1/1:
  • 2% is modulo 2 (-> 0/1)
  • 2* multiplies by 2 (-> 0/2)
  • ( decrements (-> -1/1)
    * multiplies, thus changing the sign every second time
    / divides the term on the stack by k or -k; this is the "/k!" part of the calculation together with the sign change
    X* multiplies by X; this is the "X^k" part of the calculation; we obtained the next term in the series
    _ duplicates the term to be used for calculating the following term in the next iteration
    ; (after the loop) pops the last duplicated term
    ] collects the terms on the stack in an array
    At this point we have an array [1 X -X^2/2! -X^3/3! X^4/4! X^5/5! ...] containing exactly all the terms we need for cos(x) and sin(x), interleaved
    2/ splits this array into pairs
    z transposes the matrix, resulting in the array with the terms for cos(x) and the array with the terms for sin(x), as "matrix rows"
    {...}/ again executes the block for each array item (matrix row):
  • :+ adds the elements of the matrix row together
  • 8mO6mO rounds to 86 decimals
    At this point we have the desired cos(x) and sin(x) on the stack
    p prints the representation of the last item on the stack (sin(x)) followed by a newline
    At the end of the program, the remaining contents of the stack (cos(x)) are printed automatically.

Unicode version:

"   㐵⚎ƍ뀥狒譮ꗶ㌰ꝣ⻈飖鹡䈢剺뭣䴦Ꙋ᜾"7e4b126b:c~

(the second character is a tab)

Explanation:

"..." is the ASCII version above, converted from base 126 to base 70000
7e4b treats each character as a base-70000 digit and converts the string (seen as an array of digits) to a number (7e4 = 70000)
126b converts the number to an array of digits in base 126
:c converts the numbers in the array to characters, thus the array becomes a string (which is identical to the ASCII version above)
~ evaluates the string, thus running the program

CJam - 42 ASCII / 32 Unicode

Try either version online at http://cjam.aditsu.net

First, the ASCII version:

rd:X;1_ri2*,1>{_2%2*(*/X*_}/;]2/z{:+8mO}/p

Explanation:

  • 2* multiplies by 2
  • , makes an array from 0 to (last value)-1
  • 1> removes the first item of the array (0)
  • {...}/ executes the block for each item in the array
    _ duplicates the "loop variable" (let's call it k)
    2%2*( converts from even/odd to -1/1:
  • 2% is modulo 2 (-> 0/1)
  • 2* multiplies by 2 (-> 0/2)
  • ( decrements (-> -1/1)
    * multiplies, thus changing the sign every second time
    / divides the term on the stack by k or -k; this is the "/k!" part of the calculation together with the sign change
    X* multiplies by X; this is the "X^k" part of the calculation; we obtained the next term in the series
    _ duplicates the term to be used for calculating the following term in the next iteration
    ; (after the loop) pops the last duplicated term
    ] collects the terms on the stack in an array
    At this point we have an array [1 X -X^2/2! -X^3/3! X^4/4! X^5/5! ...] containing exactly all the terms we need for cos(x) and sin(x), interleaved
    2/ splits this array into pairs
    z transposes the matrix, resulting in the array with the terms for cos(x) and the array with the terms for sin(x), as "matrix rows"
    {...}/ again executes the block for each array item (matrix row):
  • :+ adds the elements of the matrix row together
  • 8mO rounds to 8 decimals
    At this point we have the desired cos(x) and sin(x) on the stack
    p prints the representation of the last item on the stack (sin(x)) followed by a newline
    At the end of the program, the remaining contents of the stack (cos(x)) are printed automatically.

Unicode version:

"   㐵⚎ƍ뀥狒譮ꗶ㌰ꝣ⻈飖鹡䈢剺뭣䴦Ꙋ᜾"7e4b126b:c~

(the second character is a tab)

Explanation:

"..." is the ASCII version above, converted from base 126 to base 70000
7e4b treats each character as a base-70000 digit and converts the string (seen as an array of digits) to a number (7e4 = 70000)
126b converts the number to an array of digits in base 126
:c converts the numbers in the array to characters, thus the array becomes a string (which is identical to the ASCII version above)
~ evaluates the string, thus running the program

CJam - 42

rd:X;1_ri2*,1>{_2%2*(*/X*_}/;]2/z{:+6mO}/p

Try it online at http://cjam.aditsu.net

Explanation:

  • 2* multiplies by 2
  • , makes an array from 0 to (last value)-1
  • 1> removes the first item of the array (0)
  • {...}/ executes the block for each item in the array
    _ duplicates the "loop variable" (let's call it k)
    2%2*( converts from even/odd to -1/1:
  • 2% is modulo 2 (-> 0/1)
  • 2* multiplies by 2 (-> 0/2)
  • ( decrements (-> -1/1)
    * multiplies, thus changing the sign every second time
    / divides the term on the stack by k or -k; this is the "/k!" part of the calculation together with the sign change
    X* multiplies by X; this is the "X^k" part of the calculation; we obtained the next term in the series
    _ duplicates the term to be used for calculating the following term in the next iteration
    ; (after the loop) pops the last duplicated term
    ] collects the terms on the stack in an array
    At this point we have an array [1 X -X^2/2! -X^3/3! X^4/4! X^5/5! ...] containing exactly all the terms we need for cos(x) and sin(x), interleaved
    2/ splits this array into pairs
    z transposes the matrix, resulting in the array with the terms for cos(x) and the array with the terms for sin(x), as "matrix rows"
    {...}/ again executes the block for each array item (matrix row):
  • :+ adds the elements of the matrix row together
  • 6mO rounds to 6 decimals
    At this point we have the desired cos(x) and sin(x) on the stack
    p prints the representation of the last item on the stack (sin(x)) followed by a newline
    At the end of the program, the remaining contents of the stack (cos(x)) are printed automatically.
Source Link

CJam - 42 ASCII / 32 Unicode

Try either version online at http://cjam.aditsu.net

First, the ASCII version:

rd:X;1_ri2*,1>{_2%2*(*/X*_}/;]2/z{:+8mO}/p

Explanation:

r reads a token from the input
d converts to double
:X assigns to the variable X
; pops the value from the stack
1 puts 1 on the stack (the first term)
_ duplicates the 1
r reads the next token (the n)
i converts to integer
2*,1>{...}/ is a kind of loop from 1 to 2*n - 1:

  • 2* multiplies by 2
  • , makes an array from 0 to (last value)-1
  • 1> removes the first item of the array (0)
  • {...}/ executes the block for each item in the array
    _ duplicates the "loop variable" (let's call it k)
    2%2*( converts from even/odd to -1/1:
  • 2% is modulo 2 (-> 0/1)
  • 2* multiplies by 2 (-> 0/2)
  • ( decrements (-> -1/1)
    * multiplies, thus changing the sign every second time
    / divides the term on the stack by k or -k; this is the "/k!" part of the calculation together with the sign change
    X* multiplies by X; this is the "X^k" part of the calculation; we obtained the next term in the series
    _ duplicates the term to be used for calculating the following term in the next iteration
    ; (after the loop) pops the last duplicated term
    ] collects the terms on the stack in an array
    At this point we have an array [1 X -X^2/2! -X^3/3! X^4/4! X^5/5! ...] containing exactly all the terms we need for cos(x) and sin(x), interleaved
    2/ splits this array into pairs
    z transposes the matrix, resulting in the array with the terms for cos(x) and the array with the terms for sin(x), as "matrix rows"
    {...}/ again executes the block for each array item (matrix row):
  • :+ adds the elements of the matrix row together
  • 8mO rounds to 8 decimals
    At this point we have the desired cos(x) and sin(x) on the stack
    p prints the representation of the last item on the stack (sin(x)) followed by a newline
    At the end of the program, the remaining contents of the stack (cos(x)) are printed automatically.

Unicode version:

"   㐵⚎ƍ뀥狒譮ꗶ㌰ꝣ⻈飖鹡䈢剺뭣䴦Ꙋ᜾"7e4b126b:c~

(the second character is a tab)

Explanation:

"..." is the ASCII version above, converted from base 126 to base 70000
7e4b treats each character as a base-70000 digit and converts the string (seen as an array of digits) to a number (7e4 = 70000)
126b converts the number to an array of digits in base 126
:c converts the numbers in the array to characters, thus the array becomes a string (which is identical to the ASCII version above)
~ evaluates the string, thus running the program