6
\$\begingroup\$

The Challenge

Your challenge is to write a program that evaluates the following function:

f(x, t) = d^x / dt^x (sin t)

That is, the x-th derivative of sin t. In case you aren't familiar with calculus, here is a table of derivatives for the sine function:

...

f(-1, t) = -cos t

f(0, t) = sin t

f(1, t) = cos t

f(2, t)= -sin t

...

The sequence repeats these 4 functions.

The Inputs

Your program should take in an integer to be used as the x value in the function, and a float to be used at the t value in the function.

Your program may use radians or degrees for the sine function.

The Output

Your program should output the value of f(x, t) given the integer x and float t.

Scoring

This is , so the shortest answer (in bytes) wins.

\$\endgroup\$
  • 6
    \$\begingroup\$ Are you sure d^-1 / dt^-1 (sin t) = -cos t ? \$\endgroup\$ – Leaky Nun Aug 13 '16 at 18:52
  • 1
    \$\begingroup\$ What precision should this be to? \$\endgroup\$ – HyperNeutrino Aug 13 '16 at 18:58
  • 1
    \$\begingroup\$ Test cases would be great. \$\endgroup\$ – Lynn Aug 13 '16 at 22:26
  • \$\begingroup\$ Okay so trigonometry builtins seem to be allowed. Just a comment, who is not familiar with calculus does not understand the table either :P \$\endgroup\$ – Leif Willerts Aug 14 '16 at 0:46
  • \$\begingroup\$ Today, I am more in the mood of writing an exponential function e^x deriving machine. \$\endgroup\$ – sergiol May 26 '17 at 21:37

16 Answers 16

6
\$\begingroup\$

Actually, 5 bytes

╦*½+S

Try it online

Takes input as t\nx.

Explanation:

╦*½+S
╦*     pi*n
  ½    half that
   +   add t
    S  sine
\$\endgroup\$
11
\$\begingroup\$

Python, 42 bytes

from math import*
lambda x,n:sin(x+pi/2*n)

Uses the fact that differentiating shifts the function by pi/2 left.

\$\endgroup\$
  • 2
    \$\begingroup\$ I'm such an idiot \$\endgroup\$ – Leaky Nun Aug 13 '16 at 19:12
6
\$\begingroup\$

C (gcc), 30 bytes

#define f(x,n)sin(x+n*acos(0))
\$\endgroup\$
4
\$\begingroup\$

Haskell, 16 bytes

x%n=sin$x+pi/2*n

Defines a binary operator %.

λ 2.0 % (-3)
-0.4161468365471426

Uses the fact that differentiating shifts the function by pi/2 left.

Making it point-free was longer (takes n x):

(sin.).(+).(pi/2*)
\$\endgroup\$
4
\$\begingroup\$

Convex (IDE version only), 6 bytes

P½*+ms

Try it online!

After futher investigation, with the help of Dennis in chat, it was found that the command-line version of Convex (what TIO uses) has a bug that breaks the code (the link above contains a workaround). However, the IDE version of Convex, avaliable on GitHub before the challenge was posted, does not have this bug. Here is a link to the IDE version that was avaliable before the challenge was posted: Convex IDE.

\$\endgroup\$
3
\$\begingroup\$

Mathematica 14 bytes

With help from Martin Ender,

Sin[#2+Pi/2#]&
\$\endgroup\$
  • 3
    \$\begingroup\$ Shouldn't x be #2? \$\endgroup\$ – Martin Ender Aug 13 '16 at 19:54
  • 3
    \$\begingroup\$ At the same time, you don't need the *, so you'll end up with the same byte count. \$\endgroup\$ – Martin Ender Aug 13 '16 at 21:14
2
\$\begingroup\$

J, 17 bytes

4 :'1&o.d.(4|x)y'

Computes the (x%4)th derivative of sine, and then apply to right argument.

Usage:

>> f =: 4 :'1&o.d.(4|x)y'
>> _1 0 1 2 3 (f"0 0) 0
<< _1 0 1 0 _1

where >> is STDIN and << is STDOUT.

\$\endgroup\$
  • \$\begingroup\$ Why do you need to compute the (x%4)? Wouldn't it be simpler to just use the xth derivative? (Not a J user so this might be a stupid question) \$\endgroup\$ – Steven H. Aug 13 '16 at 22:45
  • \$\begingroup\$ @StevenH. Because stupid question requires negative numbers. \$\endgroup\$ – Leaky Nun Aug 14 '16 at 2:58
2
\$\begingroup\$

Julia, 17 bytes

x\t=x*π/2+t|>sin

Try it online!

\$\endgroup\$
1
\$\begingroup\$

R, 26 bytes

function(t,x)sin(t+pi/2*x)

Exactly what it says on the tin...

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 53 46 45 bytes

#import<math.h>
#define f(x,n)sin(x+M_PI/2*n)

Probably Ideone doesn't have glibc, says M_PI is undeclared.

\$\endgroup\$
  • \$\begingroup\$ M_PI is not in math.h: stackoverflow.com/questions/5007925/…. \$\endgroup\$ – orlp Aug 13 '16 at 23:00
  • \$\begingroup\$ But...it's there on glibc? sourceware.org/git/?p=glibc.git;a=blob;f=math/… \$\endgroup\$ – betseg Aug 13 '16 at 23:19
  • 1
    \$\begingroup\$ Not in "C", the language you specified in the top of your answer. C as implemented by gcc has it, so you can write "C (gcc)", but C is a language defined by an abstract standard, and makes no mention of M_PI. \$\endgroup\$ – orlp Aug 13 '16 at 23:24
1
\$\begingroup\$

JavaScript (ES6), 29 bytes

x=>t=>Math.sin(t+Math.PI/2*x)

Usage:

f=x=>t=>Math.sin(t+Math.PI/2*x)
f(x)(t)
\$\endgroup\$
1
\$\begingroup\$

Lua 5.x, 37 bytes

x,n=...return math.sin(x+math.pi/2*n)

usage:

> print(loadfile('sine.lua')(1,0))
0.8414709848079
> print(loadfile('sine.lua')(math.pi,0))
1.2246467991474e-16
> print(loadfile('sine.lua')(math.pi,1))
-1.0
> print(loadfile('sine.lua')(math.pi,2))
-2.4492935982947e-16
> print(loadfile('sine.lua')(math.pi,3))
1.0
\$\endgroup\$
1
\$\begingroup\$

Matlab, 19 bytes

@(x,t)sin(t+x*pi/2)
\$\endgroup\$
  • 1
    \$\begingroup\$ So why the downvote? \$\endgroup\$ – Luis Mendo Aug 14 '16 at 23:59
0
\$\begingroup\$

SQF, 29 bytes

Using the file-as-a-function format:

params["x","y"];sin(x+pi/2*y)

Call as [x, t] call NAME_OF_COMPILED_FUNCTION

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 25 bytes

INPUT X,N?SIN(X+N*PI()/2)

Probably shorter in some BASIC dialect that has a constant named PI rather than PI(). (Yes, PI() is not actually a function, those parentheses are a lie.)

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 7 bytes

žq*;+Ž

Port of @Mego♦'s Actually answer.

Try it online or verify some more test cases.

Explanation:

žq        # Push PI
  *       # Multiply it with the first (implicit) input `t`
   ;      # Halve it
    +     # Add the second (implicit) input `x`
     Ž   # Take the Sine of that (and output it implicitly)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.