# Print a sinusoidal wave (vertically)

Print a continuous sinusoidal wave scrolling vertically on a terminal. The program should not terminate and should continuously scroll down the wave (except until it is somehow interrupted). You may assume overflow is not a problem (i.e. you may use infinite loops with incrementing counters, or infinite recursion).

The wave should satisfy the following properties:

• Amplitude = 20 chars (peak amplitude)
• Period = 60 to 65 lines (inclusive)
• The output should only consist of spaces, newline and |
• After each line of output, pause for 50ms

Sample output:

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

The above output should go on forever unless otherwise interrupted, e.g. SIGINT or SIGKILL, or closing terminal window, or you power off your machine, or the Sun swallows the Earth, etc.

Shortest code wins.

Note. I am aware of a similar problem on Display Scrolling Waves but this isn't exactly the same. In my problem, the wave is not to be scrolled "in place" - just output it on a terminal. Also, this is an ascii-art problem, so don't use Mathematica to plot it.

• Peak amplitude, peak-to-peak amplitude, or root-square amplitude? – DavidC Jan 16 '14 at 20:31
• Peak amplitude. – user12205 Jan 16 '14 at 20:38
• Is it ok to draw a wave with just |s and no spaces? – Gelatin Jan 17 '14 at 1:23
• All answers yet are invalid. They also stop for SIGKILL not just for SIGINT. – Max Ried Apr 4 '14 at 9:02
• @Max Ried fine, I will change it to "should go on forever unless otherwise interrupted". – user12205 Apr 4 '14 at 10:28

## APL (35)

(Yes, it does fit in 35 bytes, here's a 1-byte APL encoding)

{∇⍵+⌈⎕DL.05⊣⎕←'|'↑⍨-21+⌈20×1○⍵×.1}1

Explanation:

• {...}1: call the function with 1 at the beginning
• 1○⍵×.1: close enough for government work to sin(⍵×π÷30). (1○ is sin).
• -21+⌈20: normalize to the range 1..40 and negate
• '|'↑⍨: take the last N characters from the string '|' (which results in a string of spaces with a | at the end
• ⎕←: display
• ⌈⎕DL.05: wait 50 ms and return 1. (⎕DL returns the amount of time it actually waited, which is going to be close to 0.05, rounding that value up gives 1).
• ∇⍵+: add that number (1) to and run the function again.
• Darn... I thought trigonometric functions plus the time delay would leave you guys out – user12205 Jan 17 '14 at 20:22
• Here a 33 char one: {⎕←'|'↑⍨-⌈20×1+1○⍵⋄∇.1+⍵⊣⎕DL.05}0 – Tobia Jan 18 '14 at 0:03
• @ace LOL. You should check APL out, it's not a novelty language. It's very old and has been in use in large systems for decades. It's quite unique, compared to anything else. IMHO the symbols make it much more readable that the ASCII-only derivatives (J) – Tobia Jan 18 '14 at 0:16

## C, 74737069 67 characters

67 character solution with many good ideas from @ugoren & others:

i;main(j){main(poll(printf("%*c|\n",j=21+sin(i++*.1)*20,0),0,50));}

69 character solution with while loop instead of recursion:

i;main(j){while(printf("%*c|\n",j=21+sin(i++*.1)*20,0))poll(0,0,50);}

Approaching perl territory. :)

• This was inspired by @ace's own C answer. – treamur Jan 16 '14 at 21:41
• I think you could use 5E4 instead of 50000. – musiphil Jan 17 '14 at 0:00
• I think you could use *.1 instead of /10. – moala Jan 17 '14 at 1:58
• @musiphil, I also thought about using 5E4, but it turns out that it does not work: Without showing the compiler usleep() prototype, you would have to explicitly cast the 5E4. – treamur Jan 17 '14 at 5:32
• You can cut out two more characters by moving the assignment to j into the printf, like this: printf("%*s\n",j=21+sin(i++*.1)*20,"|"). The resulting type is still int so it's a valid field width argument. – Art Jan 17 '14 at 10:17

# Mathematica 121 104 80 67 64

n=1;While[0<1,Spacer[70 Sin[n Pi/32]+70]~Print~"|";Pause@.05; n++]

• question says not to use mathematica to plot it. is this different than that somehow? – Malachi Jan 16 '14 at 23:37
• @Malachi Yes. This uses mathematica to calculate it, just like any other answer. Using mathematica to plot would be telling mathematica to plot x=20*sin(pi*y/30)+20 or something similar. – Justin Jan 16 '14 at 23:49
• ok I get what you are saying thank you for the clarification – Malachi Jan 17 '14 at 0:41
• And here is a 58 char version Do[Spacer[70*Sin[n*Pi/32]+70]~Print~"|";Pause@.05,{n,18!}] – Ajasja Jan 17 '14 at 10:22
• Im not a Mathematica user, but i think you can change 1 == 1 to 0 < 1 to decrease 1 char. – CCP Mar 24 '14 at 23:27

## Perl, 48 (68)

GNU sleep version: 48

print$"x(25+20*sin).'| ';$_+=.1;sleep .05;do$0 Cross platform: 68 use Time::HiRes"sleep";print$"x(25+20*sin).'|
';$_+=.1;sleep.05;do$0

Removed the use of Time::HiRes module by using shell sleep function. Shortened increment as per Ruby example. Shortened using $" and$0 seeing hints from Primo's work Thanks for hints Primo.

• I saved this as a file test.pl and ran perl ./test.pl, however the waiting time does not match the specification. Also, the amplitude of the wave is too small. (This amplitude refers to the length between the peak and the equilibrium position.) – user12205 Jan 16 '14 at 21:42
• I guess if I changed the increment from .105 to .1 I would beat ruby at 56 chars! – KevinColyer Jan 16 '14 at 22:50
• @primo - my shell sleep does do times shorter than 1 second... – KevinColyer Jan 17 '14 at 8:32
• man sleepunsigned int sleep(unsigned int seconds);. It won't error, but the actual sleep interval is zero. Some suggestions to make yours shorter: change $d to$_, and then use (25+20*sin), and change the \n for a literal newline. – primo Jan 17 '14 at 8:45
• @primo man 1 sleep on a GNU/Linux bash shell tells us that Unlike most implementations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number. – user12205 Jan 17 '14 at 8:58

## Perl - 64 (or 60) bytes

The following uses a Windows-specific shell command:

sleep/m50,print$"x(20.5-$_*(32-abs)/12.8),'|
'for-32..31;do$0 The following uses a GNU/Linux-specific shell command: sleep .05,print$"x(20.5-$_*(32-abs)/12.8),'| 'for-32..31;do$0

Both at 64 bytes.

• Period is 64.
• Maximum amplitude is exactly 20.
• The curve is perfectly symmetric.
• Every period is identical.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

Note that this isn't exactly a sinusoidal wave, but rather a quadratic interpolation. Plotted against an actual sin:

At the granularity required, these are visually indistinguishable.

If the aesthetics aren't so important, I offer a 60 byte alternative, with period length 62, maximum amplitude of ~20.02, and slight asymmetries:

sleep/m50,print$"x(20-$_*(31-abs)/12),'|
'for-31..30;do$0 • This isn't a sinusoidal wave; it is simply parabolas (if I read your code right). (If you can represent this with some sinusoidal wave, I'd love to see the function). – Justin Jan 21 '14 at 4:01 • Sine is a formula, if you replicate the formula it is still a Sinusoidal wave. and this is probably a variant of Sine in some fashion. – Malachi Feb 10 '14 at 15:13 # Ruby 56 i=0 loop{puts" "*(20*Math.sin(i+=0.1)+20)+?|;sleep 0.05} • Is replacing puts with p allowed? – Slicedpan Jan 17 '14 at 10:12 • @Slicedpan I think I won't, since this is a challenge to draw something. p will add double quotes around around each line and alter the "drawing". – daniero Jan 17 '14 at 15:37 # Befunge 98 - 103 100 :1g:02p' \k:02gk,'|,a,$ff*:*8*kz1+:'<\*
468:<=?ABDEFGGGHGGGFEDBA?=<:86420.,+)'&$#"!!! !!!"#$&')+,.02

Cheers for a program that does this, in a language without trigonometric capabilities; the first program in fact. The second line is simply data; the character corresponding with the ascii value of the sin, added to a space character.

EDIT: I saved 3 chars by not subtracting the space away; the sinusoid is translated 32 units to the right (which is valid).

Befunge also does not have a sleep command, or something similar. It would be nice to find a fingerprint, but I couldn't find one, so ff*:*8* pushes 8*225**2 (405000) and kz runs a noop that many times (well, that many times + 1). On windows command line with pyfunge, this turns out to be about 50 milliseconds, so I say I'm good. Note: if anyone knows a good fingerprint for this, please let me know.

The last part of the code simply checks if the counter (for the data line) is past the data, if it is, the the counter is reset to 0.

I used this to generate the data.

# Taylor Series

Although this version is 105 chars, I just had to include it:

:::f!4*jf2*-:::*:*9*\:*aa*:*:01p*-01g9*/a2*+$\f!4*j01-*b2*+:01p' \k:01gk,'|,a,ff*:*8*kz1+:f3*!3*je- I was trying to shorten my program, and decided to look at the taylor series for cosine (sine is harder to calculate). I changed x to pi * x / 30 to match the period requested here, then multiplied by 20 to match the amplitude. I made some simplifications (adjusted factors for canceling, without changing the value of the function by much). Then I implemented it. Sadly, it is not a shorter implementation. :f!4*jf2*- checks whether the values of the taylor series are getting inaccurate (about x = 15). If they are, then I compute the taylor series for x - 30 instead of x. :::*:*9*\:*aa*:*:01p*-01g9*/a2*+ is my implementation of the taylor series at x = 0, when x is the value on the stack.$\f!4*j01-*

negates the value of the taylor series if the taylor series needed adjustment.

b2*+

make the cosine wave positive; otherwise, the printing would not work.

:01p' \k:01gk,$'|,a, prints the wave ff*:*8*kz1+ makeshift wait for 50 milliseconds, then increment x :f3*!3*j$e-

If x is greater than 45, change it to -14 (again, taylor series error adjustment).

• This is exactly the kind of answer I'm looking forward to, hope you can golf it down :) – user12205 Jan 17 '14 at 8:08
• There! I successfully decreased the code length by -5 chars! And there is still room for improvement! – Justin Jan 19 '14 at 9:42
• @Quincunx my perl solution also does not use any built in trig functions ;) – primo Jan 21 '14 at 3:05

# Python, 108,93,90,89,88

import math,time
a=0
while 1:print" "*int(20+20*math.sin(a))+"|";time.sleep(.05);a+=.1

Now with infinite scrolling :)

Edit: ok, 90. Enough?

Edit:Edit: no, 89.

Edit:Edit:Edit: 88 thanks to boothby.

• Sorry if I haven't made the question clear - your program should not terminate and should continuously scroll down the wave (except until SIGINT) – user12205 Jan 16 '14 at 19:26
• a=0. -> a=0 gets you to 88 – boothby Jan 16 '14 at 23:03

# PHP, 59 characters

<?for(;;usleep(5e4))echo str_pad('',22+20*sin($a+=.1)).~ƒõ; • You can save yourself some bytes by using echo ...; in place of fwrite(STDOUT,...);. – primo Jan 17 '14 at 6:08 • That makes sense when calling from the command line anyway. 10 characters saved - thanks primo. – Alex Barrett Jan 17 '14 at 10:02 • 58: <?for(;;)echo~str_pad(ƒõ,22+20*sin($a+=.1),ß,usleep(5e4)); – primo Jan 17 '14 at 10:56
• Very nice. I won't edit my answer with those changes, you should post as your own. – Alex Barrett Jan 17 '14 at 11:05
• @ace it needs to be saved with an ansi encoding. ideone automatically converts everything to utf-8, which breaks. ~ƒõ is just shorthand for "|\n". – primo Jan 17 '14 at 11:17

# C64 BASIC, 64 PETSCII chars

On a PAL C64, For i=0 to 2:next i cycles for approx. 0,05 seconds, so the delay time is respected.

## Javascript 8876 78 characters

setInterval('console.log(Array(Math.sin(i++/10)*20+21|0).join(" ")+"|")',i=50)

Based on Kendall Frey's code.

• You never initialize i, so it prints a straight line instead of a wave. – gilly3 Jan 16 '14 at 22:37
• My mistake... It probably worked because I had ran Kendall's script already in my console, so i was initialised already for me. – joeytje50 Jan 16 '14 at 22:41

# C - 86+3 characters

Thanks shiona and Josh for the edit

i;main(j){for(;j++<21+sin(i*.1)*20;)putchar(32);puts("|");usleep(50000);i++;main(1);}

i;main(j){for(j=0;j++<20+sin(i/10.)*20;)putchar(32);puts("|");usleep(50000);i++;main();}

float i;main(j){for(j=0;j++<20+sin(i)*20;)putchar(32);puts("|");usleep(50000);i+=.1;main();}

Compiled with the -lm flag, I assume I need to add 3 chars

• Would it work if you made i an int and just divided it by 10.0 (or 9.9 to save a char?) within the call to sin()? i;main(j){for(j=0;j++<20+sin(i/10.0)*20;)putchar(32);puts("|");usleep(50000);i++;main();} – shiona Jan 16 '14 at 18:24
• You can bring the size down to 76 characters or so by using printf() to replace the for loop: printf("%*s\n",(int)(21+sin(i++/10.)*20),"|") – treamur Jan 16 '14 at 21:28
• Hmm... I would feel really guilty if I use this idea in my answer, especially when this is my own question... Would you consider posting an answer yourself? – user12205 Jan 16 '14 at 21:36
• Ok, will do. Thanks. :) – treamur Jan 16 '14 at 21:38
• You can shave off two more characters if you remove the j=0: i;main(j){for(;j++<21+sin(i/10.)*20;)putchar(32);puts("|");usleep(50000);i++;main(1);}. This relies on the assumption that the program is called with 0 arguments. – Josh Jan 16 '14 at 22:43

Ti-Basic, 33 bytes

While 1:Output(8,int(7sin(X)+8),"!":Disp "":π/30+X→X:End

The following caveats exist:

1. Due to screen limitation of 16x8, this sine wave only has an amplitude of 7 (period of 60 is still maintained)

2. Due to lack of an easy way to access the | char, ! is used instead

3. Due to lack of an accurate system timer, the delay is not implemented. However, run speed appears approximately correct.

• Heh, since TI-BASIC is counted in one-/two- byte tokens, this is actually 33 bytes (not "56 chars"), so it actually should have won the challenge! – M. I. Wright Jun 15 '15 at 4:12
• Except for the amplitude thing... – lirtosiast Jun 16 '15 at 4:01
• Well, yes, but going by bytes it's fine. – M. I. Wright Jun 16 '15 at 4:46

## JavaScript - 88

setInterval(function(){console.log(Array(Math.sin(i++/10)*20+21|0).join(" ")+"|")},i=50)

I'm sure someone can come up with something that's actually clever.

# J - 103,58,57,54

Thanks to awesome guys from IRC

(0.1&+[6!:3@]&0.05[2:1!:2~' |'#~1,~[:<.20*1+1&o.)^:_]0

In words from right to left it reads: starting from 0 infinite times do: sin, add 1 ,multiply by 20, floor, append 1 (so it becomes array of 2 elements), copy two bytes ' |' correspondingly, print it, wait 0.05s and add 0.1

Instead of infinite loop we can use recursion, it would save 2 characters, but will also produce a stack error after some number of iterations

($:+&0.1[6!:3@]&0.05[2:1!:2~' |'#~1,~[:<.20*1+1&o.)0 Where$: is a recursive call.

• Would you mind adding a little explanation, so that people unfamiliar with the J syntax (like me) can also understand your answer? – user12205 Jan 17 '14 at 8:09
• It's possible to shorten this to 50 characters by fussing about with the train's structure: (+2*(%20)6!:3@[2:1!:2~' |'#~1,~[:<.20*1+1&o.)^:_]0. The recursion version only saves 1 char this time $:@(+2*(%20)6!:3@[2:1!:2~' |'#~1,~[:<.20*1+1&o.)0 though it appears to last longer before bottoming out. – algorithmshark Feb 15 '14 at 8:28 # Haskell - 75 main=putStr$concat["|\n"++take(floor$20+20*sin x)(repeat ' ')|x<-[0,0.1..]] Unfortunately, I couldn't get the program to pause 50 ms without doubling my char count, so it just floods the console, but it does produce the sine wave. Here's the full code with pausing (138 chars with newlines): import GHC.Conc import Control.Monad main=mapM_(\a->putStr a>>threadDelay 50000)(["|\n"++take(floor$20+20*sin x)(repeat ' ')|x<-[0,0.1..]])
• Pausing was one of the requirements. Can you also post the code with the pause? – Justin Jan 16 '14 at 23:03
• Okay, I posted it. I wish Haskell let you pause code without imports. – Zaq Jan 16 '14 at 23:29
• By amplitude I mean the peak amplitude, i.e. twice the amplitude of your current program. You may wish to change it to 20+20*sin x instead to qualify. – user12205 Jan 17 '14 at 13:52
• Oh, sure. I guess I misinterpreted that part of the question. – Zaq Jan 18 '14 at 2:56

## Perl 6: 46 chars

sleep .05*say ' 'x(25+20*.sin),'|'for 0,.1...*

Create an infinite lazy Range using 0,0.1 ... *, loop over that. say returns Bool::True which numifies as 1 in multiplication, this way I can keep it in a single statement.

• I can see why sleep and .05 have to be separated. But I wonder if the space between say and ' ' is mandatory? – Matthias Jan 19 '14 at 22:35
• Yes :s It gives "2 terms in a row" error for say' ' One can use say(' ') but that's 1 char extra in this case... – Ayiko Jan 20 '14 at 18:07
• @Matthias: In Perl 6, listops either have to not take arguments, have a space after them, or use parenthesis. It's not a language designed for code golf, unlike Perl 5 (but it contains many nice builtin features, so it's usable). – Konrad Borowski Jan 22 '14 at 16:43
• @xfix Thank you for the explanation. I like the language, but I did not look into it thoroughly yet, because I still cannot use it in a work project yet. However, I always plan to write some Perl 6 scripts. @ Ayiko, I appreciate your Perl 6 posts :-) – Matthias Jan 22 '14 at 23:16

fugly Javascript - 77

i=setInterval("console.log(Array(Math.sin(i+=.1)*20+20|0).join(' ')+'|')",50)

and if we do it in Firefox - 73

i=setInterval("console.log(' '.repeat(Math.sin(i+=.1)*20+20|0)+'|');",50)

and if we're nasty - 67

i=setInterval("throw(' '.repeat(Math.sin(i+=.1)*20+20|0)+'|');",50)

# Scala, 92,89,87

• (20+20*math.sin(i*.1)) reduces it by 1 char, assuming this is valid syntax (I have no experience with Scala) – user12205 Jan 16 '14 at 21:14
• Thanks, but I have just discovered that myself :) – ValarDohaeris Jan 16 '14 at 21:20

## Python 3, 103

Stupid frikk'n imports...

import time,math
t=0
while 1:t+=(.05+t<time.clock())and(print(' '*int(20+20*math.cos(t*1.9))+'|')or.05)

Rather than "sleep", this implementation grinds at the cpu because python makes it easier to get a floating-point cpu clock than wall clock. This approach won't beat friol's, but it's fun so I'm leaving it up.

# C#

[152] Characters

I could not get the Existing C# answer to Run and I couldn't downvote because I don't have enough Reputation

it was missing a couple of { and missing a ) after the For Loop Declaration.

I figure that the variance in the look of the wave when it is run is because of the way we are trying to display this wave.

if we aren't counting the Namespace and the Method Declaration then it would be [104] characters for the working version

• The other C# answer works on gmcs. It fails to compile at first, but I think it is because there is some non-printable character in the source code. After typing it again on an empty file, the compilation is successful. – user12205 Jan 17 '14 at 16:38
• Compilers can be picky, huh? – Malachi Jan 17 '14 at 16:49

# VB [236][178]

not sure how you would count the tabs, I just took the count from Notepadd++ before I pasted here. newlines are mandatory, probably why no one likes using it for code golfing.

Module Module1
Sub Main()
Dim i
While True
Console.WriteLine("{0:" & (40 + 20 * Math.Sin(i = i + 0.1)) & "}", "|")
End While
End Sub
End Module

# C#

The Magic Line [91] Characters

Working Program Below. [148] Characters

• Sorry if I haven't made the question clear - your program should not terminate and should continuously scroll down the wave (except until SIGINT). Also, please add a character count. – user12205 Jan 16 '14 at 20:08
• Sorry forgot about that bit. Fixed now. – John ClearZ Jan 16 '14 at 20:24
• I think you can lose "Thread.Sleep"s and change "float" with var :) 117 chars. -- Sorry didn't see the wait time.. 133 chars now. using System;class P{static void Main(){for(var i=0d;;Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|'))Thread.Sleep(50);}} – Medeni Baykal Jan 16 '14 at 21:17
• I can't get it to compile in VS2010 with Threading.Thread.Sleep(50) am I doing something wrong? – Malachi Jan 17 '14 at 15:57
• I was able to get it to run, but I had to add some Brackets and Semi-colons and it doesn't look the same every period – Malachi Jan 17 '14 at 16:17

# Bash+bc (to do the math), 80

$for((;;i++)){ printf "%$(bc -l<<<"a=20*s(\$i/10);scale=0;a/1+20")s|
";sleep .05;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

# TI-BASIC, 30 bytes

Small size improvement over the other answer, at the cost of some accuracy. Note that TI-Basic technically has the | character, but you have to transfer it via computer or use an assembly program to access it.

While 1
Output(8,int(8+7sin(Ans)),":
Disp "
.03π+Ans
End
• Woah, did not see how old this challenge was! Was going to try to golf this more (which is definitely possible) but it's really not worth it... – M. I. Wright Jun 15 '15 at 4:11
• By the way, .03π can be .1, which is still within the required interval. – lirtosiast Jun 16 '15 at 3:59
• Nice catch, thanks! Do you see any way I could golf the output command? Also, since I have a CSE, I could get this to the right amplitude (26-char screen) at the cost of a few bytes. – M. I. Wright Jun 16 '15 at 4:48
• No, the output command looks fine--too bad the Disp needs a quote. The amplitude should be 20 chars, actually, making the screen width requirement 39. So it would only work on the graph screen, and there's no short way of doing that. – lirtosiast Jun 16 '15 at 5:22

## Julia - 68

Edit: thanks to M L and ace.

i=0;while 0<1;println(" "^int(20sin(.1i)+20)*"|");i+=1;sleep(.05)end

Well, it can not compete vs APL, but here's my attempt.

Output:

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
• I don't know Julia, but is it possible to use .05 instead of 0.05 in sleep? – user12205 Mar 25 '14 at 3:13
• Actually yes! Thanks – CCP Mar 25 '14 at 19:44
• Cut it down to 68 characters: i=0;while 0<1;println(" "^int(20sin(.1i)+20)*"|");i+=1;sleep(.05)end———sin(i/10)*20 is equal to 20sin(.1i) – M L Jun 20 '15 at 2:56
• I don't know Julia, but could you use a for loop iterating over all the natural numbers instead? – lirtosiast Jun 22 '15 at 3:31

# MATLAB, 81 bytes

t=0;while(fprintf('%s\n',i))i=[];t=t+1;i(fix(21+20*sind(t*6)))='|';pause(.05);end

I abused the fact that i is always initialized in MATLAB, which meant that I could put the fprintf in the while statement without initializing i first. This does mean the program first outputs an empty line, but I think this is not forbidden in the spec.

Furthermore, it abuses the fact that Matlab will ignore most ASCII control characters, printing a space instead of NULL (also for the first empty line).

• "I abused the fact that i is always initialized in MATLAB, which meant that I could put the fprintf in the while statement without initializing i first." Really clever! +1! – Stewie Griffin Jan 20 '16 at 21:20

## F# - 907977 76

Here's a solution using recursion

let rec f x=printfn"%*c"(int(20.*sin x)+21)'|';Thread.Sleep 50;f(x+0.1)
f 0.

It could probably be improved further.

• Without knowing anything about F#, I'm assuming that Thread.Sleep expects a value in ms, so you can get rid of one of the 0's and do Thread.Sleep 50. :) – ValarDohaeris Jan 17 '14 at 0:49
• @ValarDohaeris You're right. I misread the requirements. – p.s.w.g Jan 17 '14 at 0:54

## AutoHotkey 176

SetKeyDelay,-1
p:=0
loop
{
sleep 50
p+=Mod(Floor(A_index/40),2)?-1:1,t:=""
loop % p
t .= " "
sendinput % t "|n"
}
esc::Exitapp

Run the script . It opens Notepad and prints the characters. Press Esc anytime to exit.

Clojure, 121

Short version:

(loop[a 0](println(clojure.string/join(repeat(int(+ 20 (* 20 (Math/sin a)))) " ")) \|)(Thread/sleep 50)(recur(+ a 0.1)))

Pretty version:

(loop [a 0]
(println (clojure.string/join (repeat (int (+ 20 (* 20 (Math/sin a)))) " ")) \|)