Print the sequence
Scratch 3.0 39 blocks/323 bytes
Try it online scratch!
Alternatively, as SB syntax:
when gf clicked
delete[all v]of[o v
ask()and wait
set[. v]to(answer
set[n v]to(1
repeat until<(n)contains[.
if<((n)mod(4))=(0
set[. v]to((. )*(n
else
if<((n)mod(4))=(1
change[. v]by(n
else
if<((n)mod(4))=(2
change[. v]by((0)-(n
else
set[. v]to((. )/(n
end
end
end
add(n)to[o v
change[n v]by(1
end
delete(length of(o))of[o v
Look at you guys, having fun with your fancy eval
statements! Well, not me! No... Scratch doesn't have evals, so I had to do things the hard way... if statements.
At least it isn't goto
s...
Haskell, 75 74 73 bytes
-1 byte thanks to Will Ness -1 byte thanks to nimi
(#1)
n#i|i`mod`4<1,n`mod`i>0=[n]|y<-i+1=n:(x!!i)n i#y
x=div:(*):(+):(-):x
Try it online!
Avoids the use of fractional ints to save on bytes
Whitespace, 251 227 202 bytes
[S S S N
_Push_0][S N
S _Duplicate_0][S N
S _Duplicate_0][T N
T T _Read_STDIN_as_integer][T T T _Retrieve_input][N
S S N
_Create_Label_LOOP][S N
S _Duplicate_top][T N
S T _Print_as_integer][S S S T S T S N
_Push_10_newline][T N
S S _Print_as_character][S N
T _Swap_top_two][S S S T N
_Push_1][T S S S _Add][S T S S T N
_Copy_2nd_item][S T S S T N
_Copy_2nd_item][S N
S _Duplicate_top][S S S T S S N
_Push_4][T S T T _Modulo][S N
S _Duplicate_top][N
T S S N
_If_0_Jump_to_Label_DIVIDE][S S S T N
_Push_1][T S S T _Subtract][S N
S _Duplicate_top][N
T S T N
_If_0_Jump_to_Label_MULTIPLY][S S S T N
_Push_1][T S S T _Subtract][N
T S S S N
_If_0_Jump_to_Label_ADD][S N
T _Swap_top_two][S T S S T N
_Copy_2nd_item][T S S T _Subtract][N
S N
N
_Jump_to_LOOP][N
S S S N
_Create_Label_DIVIDE][S N
N
_Discard_top][T S T S _Divide][S T S S T S N
_Copy_3nd_item][S T S S T S N
_Copy_3nd_item][T S T T _Modulo][N
T S N
_If_0_Jump_to_Label_LOOP][N
N
N
_Exit_Program][N
S S T N
_Create_Label_MULTIPLY][S N
N
_Discard_top][T S S N
_Multiply][N
S N
N
_Jump_to_Label_LOOP][N
S S S S N
_Create_Label_ADD][T S S S _Add][N
S N
N
_Jump_to_Label_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.
[..._some_action]
added as explanation only.
Try it online (with raw spaces, tabs and new-lines only).
-24 bytes after a comment of @JoKing suggesting n%i > 0
. Although only if(x < 0)
and if(x == 0)
are available in Whitespace, simply checking if(x*-1 < 0)
is basically the same as if(x > 0)
.
An additional -25 bytes thanks to @JoKing.
Explanation:
Quote from the Scratch answer:
At least it isn't
goto
s...
Did someone say goto
? Whitespace has nothing else than goto
to create both loops and if-statements. xD In addition it's a stack-based language, so I have to swap/discard/copy pretty often. And to top things off: Whitespace doesn't even have floating points and only integer-division, so I've used n % i * -1 < 0
to exit the program if the integer cannot divide the current number.
Pseudo-code:
Integer n = STDIN as input
Integer i = 0
Label LOOP:
Print n as number
i = i + 1
Integer t = i % 4
If(t == 0):
Jump to Label DIVIDE
t = t - 1
If(t == 0):
Jump to Label MULTIPLY
t = t - 1
If(t == 0):
Jump to Label ADD
n = n - i
Jump to Label LOOP
Label DIVIDE:
n = n / i
Integer m = n % i
If(m == 0):
Jump to Label LOOP
Exit program
Label MULTIPLY:
n = n * i
Jump to Label LOOP
Label ADD:
n = n + i
Jump to Label LOOP