39
\$\begingroup\$

Your very own "for" instruction

Assuming you have the following input : a, b, c, d

Input can be in one-line using any format "a/b/c/d" or "a,b,c,d" etc..

You can also have 4 inputs.

You must code the following behaviour (pseudo-code here) :

var i = <a>
while (i <b> <c>)
    print i
    i = i + <d>
    print "\n"

Here are some tests cases :

input : 1,<,10,1
output :
1
2
3
4
5
6
7
8
9

One more :

input : 20,>,10,1
output :
20
21
22
23
24
25
26
27
...
infinite loop / program crashes
  • a is an integer, the initial value of i.

  • b is a string or a char, it can't be something else, the comparator used in the ending condition of the for loop.

    b can and must be one of the following strings :

    - ">"
    - "<"
    
  • c is an integer, the number used in the ending condition of the for loop.

  • d is an integer that is added to i at every loop.

This is code-golf, the shortest answer wins !

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Can the numbers be returned from a function as a list/sequence, rather than printed to stdout? \$\endgroup\$
    – smls
    Jan 26, 2017 at 17:07
  • \$\begingroup\$ @smls Nope sorry, the output must be like the examples ! \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 20:37
  • 1
    \$\begingroup\$ It says my code should follow the pseudo-code and there's a print "\n", but I am using javascript's alert for each line. Would that be acceptable, or would I have to use console.log instead making my answer longer? \$\endgroup\$
    – user64039
    Jan 27, 2017 at 10:46
  • 2
    \$\begingroup\$ You can use the alert function as a way to ouput but you can't use multiple alerts. Something like alert("23\n24\n25"); would work whereas alert("23"); alert("24"); alert(25); wouldn't \$\endgroup\$
    – Sygmei
    Jan 27, 2017 at 10:56

39 Answers 39

25
\$\begingroup\$

JavaScript (ES6),  44  43 56 bytes

Saved 1 byte thanks to ETHproductions
Edit: fixed to comply with output requirements

(a,b,c,d)=>{for(s='';eval(a+b+c);a+=d)s+=a+`
`;alert(s)}

Test

let f =

(a,b,c,d)=>{for(s='';eval(a+b+c);a+=d)s+=a+`
`;alert(s)}

f(1,'<',7,2)

\$\endgroup\$
7
  • \$\begingroup\$ Nice use of scope! \$\endgroup\$ Jan 26, 2017 at 13:46
  • \$\begingroup\$ I think you can rearrange the eval to save a byte: (a,b,c,d)=>{for(;eval(a+b+c);a+=d)alert(a)} \$\endgroup\$ Jan 26, 2017 at 13:48
  • \$\begingroup\$ @ETHproductions Ah, yes. Nice one! \$\endgroup\$
    – Arnauld
    Jan 26, 2017 at 13:52
  • 5
    \$\begingroup\$ That's a 44 with a tutu! \$\endgroup\$
    – aross
    Jan 26, 2017 at 16:24
  • \$\begingroup\$ This doesn't follow the specification where output is line-by-line with U+000A after every line. \$\endgroup\$
    – Joey
    Jan 27, 2017 at 10:36
17
\$\begingroup\$

Javascript (ES6), 47 42 48 Bytes

Wanted to make the for version but someone was faster, so here's the recursive version.

(b,c,d)=>F=a=>eval(a+b+c)&&console.log(a)|F(a+d)

You need to add f= before and call it like f(b,c,d)(a).

Many thanks to Arnauld for the awesome golf.

alert changed to console.log because of output specification

\$\endgroup\$
5
  • \$\begingroup\$ @Arnauld Thanks, that's a pretty cool golf. I just asked him, so let's see if he accepts it. \$\endgroup\$
    – user64039
    Jan 26, 2017 at 15:20
  • \$\begingroup\$ Glad to see it accepted. ;) \$\endgroup\$
    – Arnauld
    Jan 26, 2017 at 15:44
  • \$\begingroup\$ This doesn't follow the specification where output is line-by-line with U+000A after every line. \$\endgroup\$
    – Joey
    Jan 27, 2017 at 10:36
  • \$\begingroup\$ @Joey That's just pseudo-code, but I'll ask OP about this. \$\endgroup\$
    – user64039
    Jan 27, 2017 at 10:47
  • \$\begingroup\$ @Masterzagh: There was a question about alternative output formats already which was denied. \$\endgroup\$
    – Joey
    Jan 27, 2017 at 10:49
15
\$\begingroup\$

Pure bash, 35

I assume its OK just to plug the parameters into the standard for loop:

for((i=$1;i$2$3;i+=$4));{ echo $i;}

Try it online.

\$\endgroup\$
1
  • \$\begingroup\$ Haha, bash makes it really easy \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 20:39
13
\$\begingroup\$

Jelly, 12 bytes

Ṅ+⁶µ⁴;⁵¹vµ¿t

Try it online!

Jelly has a lot of ways to tersely do iteration, create ranges, etc.. However, mirroring C++'s behaviour exactly is fairly hard, due to special cases like the increment being 0, the loop ending before it starts (due to the inequality being backwards), and the increment going in the wrong direction (thus meaning the exit condition of the loop can't be met naturally). As such, this solution is basically a direct translation of the C++, even though that makes it rather more low-level than a Jelly program normally is. Luckily, C++ has undefined behaviour on signed integer overflow (the question uses int), meaning that a program can do anything in that case, and thus there's no need to try to mimic the overflow behaviour.

Explanation

Ṅ+⁶µ⁴;⁵¹vµ¿t
   µ     µ¿   While loop; while ((⁴;⁵¹v) counter) do (counter = (Ṅ+⁶)counter).
    ⁴;⁵       Second input (b) appended to third input (c), e.g. "<10"
        v     Evaluate, e.g. if the counter is 5, "<10" of the counter is true
       ¹      No-op, resolves a parser ambiguity
Ṅ             Output the counter, plus a newline
 +⁶           Add the fourth input (d)
           t  Crashes the program (because the counter is not a list)

Crashing the program is the tersest way to turn off Jelly's implicit output (otherwise, it would output the final value of the counter); it generates a bunch of error messges on stderr, but we normally consider that to be allowed.

Incidentally, the loop counter is initialised with the current value before the loop starts. As the loop appears at the start of the program, that'll be the first input.

\$\endgroup\$
6
  • \$\begingroup\$ You could change t to to have no crash. The dequeue results in an empty list for which Jelly's implicit print yields nothing. \$\endgroup\$ Jan 26, 2017 at 15:19
  • \$\begingroup\$ @JonathanAllan: It doesn't, what it actually does is to create a range from 2 to the given value, which is definitely visible on an implicit print. \$\endgroup\$
    – user62131
    Jan 26, 2017 at 16:01
  • \$\begingroup\$ Ah, I must've tested that theory with a loop ending in negative territory; indeed a range is implicitly created. \$\endgroup\$ Jan 26, 2017 at 16:26
  • \$\begingroup\$ Uhm, this is 12 characters, but it's not 12 bytes right? \$\endgroup\$
    – Cruncher
    Jan 27, 2017 at 22:44
  • \$\begingroup\$ @Cruncher: Jelly uses its own encoding in which each character used by the language is represented by a single byte (it only uses 256 different characters). The reason it doesn't use something better-known like code page 437 is to make it easier to type (I mean, it's not that easy to type, but it's easier than a language like gs2 would be). A hexdump of this program would be 12 bytes long. \$\endgroup\$
    – user62131
    Jan 27, 2017 at 23:26
12
\$\begingroup\$

Python 2, 50 bytes

a,b,c,d=input()
while eval(`a`+b+`c`):print a;a+=d

Try it online!

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

R, 63 bytes

function(a,b,c,d)while(do.call(b,list(a,c))){cat(a,"\n");a=a+d}
\$\endgroup\$
9
\$\begingroup\$

Java, 58 bytes

(a,b,c,d)->{for(;b>61?a>c:a<c;a+=d)System.out.println(a);}
\$\endgroup\$
7
  • 14
    \$\begingroup\$ Is there a reason to create i? Could you skip the initialization part and just use a? Also, using the ASCII value of '>' (62) saves a byte. \$\endgroup\$
    – Riley
    Jan 26, 2017 at 14:22
  • 6
    \$\begingroup\$ Following Riley's comment, you can do b>61 \$\endgroup\$
    – user41805
    Jan 26, 2017 at 16:55
  • \$\begingroup\$ I don't believe this compiles. \$\endgroup\$ Jan 28, 2017 at 21:06
  • \$\begingroup\$ @ChiefTwoPencils It's a function. You have to write a test program around it in order to compile it. \$\endgroup\$
    – wizzwizz4
    Jan 29, 2017 at 18:42
  • \$\begingroup\$ @wizzwizz4, obviously. But that still doesn't work. Give it a shot. Plus, my understanding is all bytes required to run it counts. \$\endgroup\$ Jan 30, 2017 at 23:49
7
\$\begingroup\$

05AB1E, 22 20 bytes

[D²`'>Q"‹›"è.V_#D,³+

Try it online!

Explanation

[                       # start loop
 D                      # copy top of stack (current value of a)
  ²`                    # push b,c to stack
    '>Q                 # compare b to ">" for equality
       "‹›"             # push this string
           è            # index into the string with this result of the equality check
            .V          # execute this command comparing a with c
              _#        # if the condition is false, exit loop (and program)
                D,      # print a copy of the top of the stack (current value of a)
                  ³+    # increment top of stack (a) by d
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Any input format is accepted so the second version is okay :) \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 15:09
7
\$\begingroup\$

SmileBASIC, 53 bytes

INPUT A,B$,C,D
S=ASC(B$)-61WHILE S*A>S*C?A
A=A+D
WEND

Explanation:

INPUT A,B$,C,D
IF B$=="<" THEN S=-1 ELSE S=1 'get comparison direction
I=A
WHILE S*I>S*C 'loop while I is less than/greater than the end
 PRINT I
 INC I,D
WEND

This uses the fact that X<Y is the same as -X>-Y

\$\endgroup\$
8
  • \$\begingroup\$ I'll trust you for this one, I don't have a 3DS to test :) \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 13:52
  • \$\begingroup\$ I have Petit Computer, so cool idea! I will try something like this sometime... \$\endgroup\$
    – python-b5
    Jan 26, 2017 at 21:24
  • \$\begingroup\$ You could use a READ statement, saving 1 byte. \$\endgroup\$
    – ckjbgames
    Jan 26, 2017 at 22:06
  • \$\begingroup\$ @ckjbgames how? \$\endgroup\$
    – 12Me21
    Jan 26, 2017 at 22:08
  • \$\begingroup\$ @12Me21 Check the SmileBASIC manuals. It should be in the list of instructions for SmileBASIC. \$\endgroup\$
    – ckjbgames
    Jan 26, 2017 at 22:24
6
\$\begingroup\$

Stacked, 34 bytes

@d@c@b[show d+][:c b tofunc!]while

Try it online! (Testing included.) This is a function that expects the stack to look like:

a b c d

For example:

1 '<' 10 2
@d@c@b[show d+][:c b tofunc!]while

Explanation

@d@c@b[show d+][:c b tofunc!]while
@d@c@b                               assign variables
               [............]while   while:
                :c                   duplicate "i" and push c
                   b tofunc!         convert b to a function and execute it
      [.......]                      do:
       show                          output "i" without popping
            d+                       and add the step to it
\$\endgroup\$
6
\$\begingroup\$

Plain TeX, 88 bytes

\newcount\i\def\for#1 #2 #3 #4 {\i#1\loop\the\i\endgraf\advance\i#4\ifnum\i#2#3\repeat} 

The command \for provides the requested function. Save this as for.tex and then run it and enter the variable values at the command line: pdftex '\input for \for 1 < 5 1 \bye' The variable values must be separated by spaces.

\$\endgroup\$
5
\$\begingroup\$

Python 2, 45 bytes

exec"i=%d\nwhile i%c%d:print i;i+=%d"%input()

Try it online!

A very literal implementation of the spec. Takes the code template, substitutes in the inputs via string formatting, and executes it.

\$\endgroup\$
4
\$\begingroup\$

C++, 80

Whoops, this is C++ not C. Was a bit confused by the question.

void f(int a,char b,int c,int d){for(;b==62?a>c:a<c;a+=d)cout<<a<<endl;}
\$\endgroup\$
7
  • \$\begingroup\$ Is this C or C++? \$\endgroup\$
    – betseg
    Jan 26, 2017 at 14:00
  • 10
    \$\begingroup\$ Which implementation of C++? (I'm curious how you're getting something akin to using namespace std for free). \$\endgroup\$
    – H Walters
    Jan 26, 2017 at 14:13
  • \$\begingroup\$ Doesn't i have to start at a, not 0? You can just use a and skip i altogether and use the ASCII value of '>'. for(;b==62?a>c:a<c;a+=d) \$\endgroup\$
    – Riley
    Jan 26, 2017 at 14:18
  • \$\begingroup\$ Doesn't work for f(1,'<'3,1); \$\endgroup\$ Jan 26, 2017 at 14:34
  • \$\begingroup\$ Ack... yeah, requires the math on both sides; for(b-=61;b*a>b*c;a+=d) works for a single byte; but so does for(;b-62?a<c:a>c;a+=d). \$\endgroup\$
    – H Walters
    Jan 26, 2017 at 14:46
4
\$\begingroup\$

C, 52 51 bytes

-1 byte thanks to H Walters

f(a,b,c,d){for(;b&2?a>c:a<c;a+=d)printf("%d\n",a);}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Sorry for the mistake in the pseudo-code, i increments after each print :) \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 15:57
  • 1
    \$\begingroup\$ Use b&2 instead of b^60 for another byte. \$\endgroup\$
    – H Walters
    Jan 26, 2017 at 16:02
4
\$\begingroup\$

Python 3, 52 bytes

def f(a,b,c,d):
 while[a>c,a<c][b<'>']:print(a);a+=d

repl.it

\$\endgroup\$
1
  • \$\begingroup\$ Clever use of lists ! \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 20:42
4
\$\begingroup\$

Pip, 14 bytes

W Va.b.ca:d+Pa

Takes four command-line arguments. Supports negative & floating point numbers and comparison operators < > = <= >= !=. Try it online!

                a,b,c,d are cmdline args
W               While loop with the following condition:
  Va.b.c          Concatenate a,b,c and eval
            Pa  Print a with newline (expression also returns value of a)
        a:d+    Add d to that and assign back to a
\$\endgroup\$
4
\$\begingroup\$

Jelly, 8 bytes

ḢṄ+⁹;µV¿

This is a dyadic link that takes a,b,c as its left argument and d as its right one. Output may be infinite and goes to STDOUT.

Try it online!

How it works

ḢṄ+⁹;µV¿  Dyadic link.
          Left argument:  a,b,c (integer, character, integer)
          Right argument: d     (integer)

       ¿  While...
      V     the eval atom applied to a,b,c returns 1:
     µ       Combine the links to the left into a chain and apply it to a,b,c.
Ḣ              Head; pop and yield a from a,b,c.
 Ṅ             Print a, followed by a linefeed.
  +⁹           Add a and the right argument (d) of the dyadic link.
    ;          Concatenate the result and the popped argument of the chain,
               yielding a+d,b,c.
\$\endgroup\$
2
  • \$\begingroup\$ Command-line arguments use Python syntax and cannot distinguish between a character and a singleton string. If you want to use CLAs, you have to insert an F to flatten the array. \$\endgroup\$
    – Dennis
    Jan 26, 2017 at 23:47
  • 2
    \$\begingroup\$ Now I want to delete half my comment as it's obsolete, whilst keeping the other half. I guess I'll just repeat the relevant half and delete the rest: "Oh, bleh, you defined it as a function so you could disregard the implicit output under PPCG rules. I should have thought of that." \$\endgroup\$
    – user62131
    Jan 26, 2017 at 23:56
4
\$\begingroup\$

Python 3, 61 bytes

One liner:

e=input;exec(f'i={e()}\nwhile i{e()}{e()}:print(i);i+={e()}')
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the site! Nice use of the new literal string interpolation feature. I think you might be able to save a byte by replacing \t with a space. \$\endgroup\$
    – 0 '
    Jan 28, 2017 at 5:56
  • \$\begingroup\$ Thank you.still the same size after removing the \n\t after third e() \$\endgroup\$
    – G-Ox7cd
    Jan 28, 2017 at 6:37
4
\$\begingroup\$

Common Lisp, 82 80 79 73 64 bytes

(defmacro f(a b c d)`(do((i,a(+ i,d)))((not(,b i,c)))(print i)))

Test

(f 1 < 10 1)

1 
2 
3 
4 
5 
6 
7 
8 
9 
NIL
CL-USER> 

-9 bytes thanks to PrzemysławP.

\$\endgroup\$
2
  • \$\begingroup\$ Perhaps you can save 9 bytes, by defining a macro. (defmacro f(a b c d)<insert backqoute here>(do((i,a(+ i,d)))((not(,b i,c)))(print i))) Usage: (f 1 < 10 1) \$\endgroup\$
    – user65167
    May 1, 2017 at 22:55
  • \$\begingroup\$ @PrzemysławP Thanks again ! \$\endgroup\$
    – coredump
    Jun 7, 2017 at 8:24
3
\$\begingroup\$

Haskell, 66 64 bytes

f a b c d|last$(a<c):[a>c|b>"<"]=print a>>f(a+d)b c d|1<3=pure()

Try it online! Usage:

Prelude> f 0 "<" 9 2
0
2
4
6
8
\$\endgroup\$
3
\$\begingroup\$

Bash (+Unix Tools), 29 bytes

Golfed

bc<<<"for(x=$1;x$2$3;x+=$4)x"

Test

./forloop 1 '<' 10 1
1
2
3
4
5
6
7
8
9
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Ha. I was just about to post the exact same thing! +1 \$\endgroup\$ Jan 26, 2017 at 22:07
3
\$\begingroup\$

Ruby, 43 41 bytes

->a,*i,d{a=d+p(a)while eval"%s"*3%[a,*i]}

If b can be taken in as a Ruby symbol instead of a string, you get 38 bytes:

->a,b,c,d{a=d+p(a)while[a,c].reduce b}

Try either solution online!

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

PHP, 69 65 bytes

for(list(,$i,$b,$c,$d)=$argv);$b<"="?$i<$c:$i>$c;$i+=$d)echo"$i
";

Run with '-r'; provide command line arguments as input.

For just one byte more 4 more bytes, I can take every operator:

for(list(,$i,$b,$c,$d)=$argv;eval("return $i$b$c;");$i+=$d)echo"$i
";

Yeah, evil eval. Did you know that it can return something?


Shorthand destructuring [,$i,$b,$c,$d]=$argv; would save 4 more bytes;
but PHP 7.1 postdates the challenge.

\$\endgroup\$
4
  • \$\begingroup\$ Neat ! I wasn't sure when creating the challenge if I should include every common operators, then I remembered that they aren't all the same (~= for != in Lua for example) \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 20:55
  • \$\begingroup\$ Woah, eval IS evil. \$\endgroup\$
    – cyberbit
    Jan 27, 2017 at 15:29
  • \$\begingroup\$ It seems to me that you can use PHP 7.1 to make it shorter. If it is not so the use of list saves 4 Bytes plus 4 Bytes with short syntax \$\endgroup\$ Jun 7, 2017 at 11:32
  • \$\begingroup\$ @PHP 7.1 postdates the challenge; but thanks for list(). \$\endgroup\$
    – Titus
    Jun 7, 2017 at 16:23
2
\$\begingroup\$

Perl 6, 44 bytes

{.say for $^a,*+$^d...^*cmp$^c!= $^b.ord-61}

How it works

{                                          }  # A lambda.
          $^a                                 # Argument a.
             ,*+$^d                           # Iteratively add d,
                   ...^                       # until (but not including the endpoint)
                       *cmp$^c                # the current value compared to c
                                              # (less=-1, same=0, more=1)
                              != $^b.ord-61.  # isn't the codepoint of the b minus 61.
 .say for                                     # Print each number followed by a newline.

If it's okay to return a (potentially infinite) sequence of numbers as a value of type Seq, instead of printing the numbers to stdout, the .say for part could be removed, bringing it down to 35 bytes.

\$\endgroup\$
2
\$\begingroup\$

Clojure, 66 63 bytes

#(when((if(= %2"<")< >)% %3)(println %)(recur(+ % %4)%2 %3 %4))

-3 bytes by factoring out the loop. I'm "abusing" the init parameter to act as the running accumulator.

Recursive solution (with TCO). See comments in pregolfed code. I tried a non-TCO recursive solution, and it ended up being 67 bytes.

I'd love to see this beat in Clojure! I think this is the smallest I can get it.

(defn my-for [init-num com-str com-num inc-num]
  (let [op (if (= com-str "<") < >)] ; Figure out which operator to use
    (when (op init-num com-num) ; When the condition is true, print and recur
      (println init-num)
      (recur (+ init-num inc-num) com-str com-num inc-num))))
    ; Else, terminate (implicit) 
\$\endgroup\$
1
  • \$\begingroup\$ Oh I didn't notice this answer. #(when(({">">"<"<}%2)% %3)(println %)(recur(+ % %4)%2 %3 %4)) would be 61 bytes, combining your when with my ({">">"<"<}%2). \$\endgroup\$
    – NikoNyrh
    Jan 26, 2017 at 19:23
2
\$\begingroup\$

Groovy, 51 bytes

{a,b,c,d->while(Eval.me("$a$b$c")){println a;a+=d}}

This is an unnamed closure. Try it Online!

Caution - If you want to test this with groovy console, make sure you kill the entire process when the input causes an infinite loop. I noticed this after it consumed ~5 gigs of RAM.

\$\endgroup\$
2
\$\begingroup\$

QBIC, 51 40 bytes

:;::{?a┘a=a+c~A=@<`|~a>=b|_X]\~a<=b|_X

And three minutes after posting I realised I could simplify the terminator logic...

:;::      Consecutively read a, A$, b and c from the command line
{?a┘      Start an infinite loop; print a, add a newline to the source
a=a+c     increment a
~A=@<`|   If we are in LESS THAN mode
  ~a>=b   and IF we are no longer LESS
    |_X]  THEN QUIT, end if.
  \       ELSE (we're in GREATER THAN mode)
    ~a<=b IF we are no longer GREATER
    |_X   THEN QUIT
          The last IF and the loop are auto-closed
\$\endgroup\$
2
\$\begingroup\$

Batch, 94 bytes

@set i=%1
@set o=gtr
@if "%~2"=="<" set o=lss
:g
@if %i% %o% %3 echo %i%&set/ai+=%4&goto g

If it wasn't for the second parameter behaviour, it could be done in 53 bytes:

@for /l %%i in (%1,%4,%n%)do @if not %%i==%3 echo %%i

This simply does nothing if the step has the wrong sign. The extra test is because Batch's for loop allows the loop variable to equal the end value.

\$\endgroup\$
2
\$\begingroup\$

Clojure, 66 bytes

#(loop[i %](if(({">">"<"<}%2)i %3)(do(println i)(recur(+ i %4)))))

This could have been 55 bytes as< and > are functions in Clojure:

(def f #(loop[i %](if(%2 i %3)(do(println i)(recur(+ i %4))))))
(f 1 < 10 1)
\$\endgroup\$
8
  • \$\begingroup\$ I like the use of the map here. I would have never thought that that would have beaten my way. Also interesting that both of our initial counts were the same, despite slightly different approaches. \$\endgroup\$ Jan 26, 2017 at 20:30
  • \$\begingroup\$ Allowing b to be a function would give an unfair advantage to some languages :) \$\endgroup\$
    – Sygmei
    Jan 26, 2017 at 20:40
  • \$\begingroup\$ True, but I think most languages I know of wouldn't benefit much from allowing < instead of "<", except Clojure. \$\endgroup\$
    – NikoNyrh
    Jan 26, 2017 at 20:43
  • \$\begingroup\$ @Sygmei True. It would be freakin sweet though. Can't blame you making that call. \$\endgroup\$ Jan 26, 2017 at 20:43
  • \$\begingroup\$ OP said characters are fine instead of strings for the comparison operators btw. That should save a couple bytes. \$\endgroup\$ Jan 26, 2017 at 20:44
2
\$\begingroup\$

TI-Basic, 41 34 bytes

Prompt A,Str2,Str3,D
While expr("A"+Str2+Str3
Disp A
A+D->A
End
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The way that a TI calculator works, many symbols are stored as a single byte. Prompt , Str2, Str3, While , expr(, Disp , ->, and End are all single-byte symbols. I count 29 bytes. \$\endgroup\$
    – Pavel
    Jan 27, 2017 at 0:14
  • \$\begingroup\$ @Pavel Thanks for your interest! Although it's true that TI-Basic is tokenized, not all tokens are one byte. For example, Str2, Str3, and expr( are all two-byte tokens. To see a list of one-byte tokens, check out tibasicdev.wikidot.com/one-byte-tokens \$\endgroup\$
    – Timtech
    Jan 27, 2017 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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