The inverse Collatz Conjecture
up vote
14
down vote
favorite
I think the Collatz Conjecture is already well-known. But what if we invert the rules?
Start with an integer n >= 1.
Repeat the following steps:
If n is even, multiply it by 3 and add 1.
If n is odd, subtract 1 and divide it by 2.
Stop when it reaches 0
Print the iterated numbers.
Test cases:
1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...
Rules:
This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.
I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.
You need to print the initial input as well.
The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.
The smallest code wins.
code-golf math integer
|
show 1 more comment
up vote
14
down vote
favorite
I think the Collatz Conjecture is already well-known. But what if we invert the rules?
Start with an integer n >= 1.
Repeat the following steps:
If n is even, multiply it by 3 and add 1.
If n is odd, subtract 1 and divide it by 2.
Stop when it reaches 0
Print the iterated numbers.
Test cases:
1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...
Rules:
This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.
I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.
You need to print the initial input as well.
The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.
The smallest code wins.
code-golf math integer
9
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
Nov 4 at 20:38
Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
Nov 4 at 21:38
Do we have to print the0
at the end?
– flawr
Nov 4 at 23:01
2
You might want to expand the last two test cases, since they're not that long
– Jo King
Nov 4 at 23:07
2
@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number.
– Eduardo Hoefel
Nov 5 at 2:40
|
show 1 more comment
up vote
14
down vote
favorite
up vote
14
down vote
favorite
I think the Collatz Conjecture is already well-known. But what if we invert the rules?
Start with an integer n >= 1.
Repeat the following steps:
If n is even, multiply it by 3 and add 1.
If n is odd, subtract 1 and divide it by 2.
Stop when it reaches 0
Print the iterated numbers.
Test cases:
1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...
Rules:
This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.
I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.
You need to print the initial input as well.
The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.
The smallest code wins.
code-golf math integer
I think the Collatz Conjecture is already well-known. But what if we invert the rules?
Start with an integer n >= 1.
Repeat the following steps:
If n is even, multiply it by 3 and add 1.
If n is odd, subtract 1 and divide it by 2.
Stop when it reaches 0
Print the iterated numbers.
Test cases:
1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...
Rules:
This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.
I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.
You need to print the initial input as well.
The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.
The smallest code wins.
code-golf math integer
code-golf math integer
edited Nov 4 at 21:41
asked Nov 4 at 20:24
Eduardo Hoefel
367112
367112
9
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
Nov 4 at 20:38
Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
Nov 4 at 21:38
Do we have to print the0
at the end?
– flawr
Nov 4 at 23:01
2
You might want to expand the last two test cases, since they're not that long
– Jo King
Nov 4 at 23:07
2
@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number.
– Eduardo Hoefel
Nov 5 at 2:40
|
show 1 more comment
9
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
Nov 4 at 20:38
Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
Nov 4 at 21:38
Do we have to print the0
at the end?
– flawr
Nov 4 at 23:01
2
You might want to expand the last two test cases, since they're not that long
– Jo King
Nov 4 at 23:07
2
@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number.
– Eduardo Hoefel
Nov 5 at 2:40
9
9
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
Nov 4 at 20:38
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
Nov 4 at 20:38
Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
Nov 4 at 21:38
Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
Nov 4 at 21:38
Do we have to print the
0
at the end?– flawr
Nov 4 at 23:01
Do we have to print the
0
at the end?– flawr
Nov 4 at 23:01
2
2
You might want to expand the last two test cases, since they're not that long
– Jo King
Nov 4 at 23:07
You might want to expand the last two test cases, since they're not that long
– Jo King
Nov 4 at 23:07
2
2
@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number.
– Eduardo Hoefel
Nov 5 at 2:40
@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number.
– Eduardo Hoefel
Nov 5 at 2:40
|
show 1 more comment
25 Answers
25
active
oldest
votes
up vote
5
down vote
Perl 6, 30 bytes
{$_,{$_%2??$_+>1!!$_*3+1}...0}
Try it online!
Anonymous code block that returns a sequence.
Explanation:
{$_,{$_%2??$_+>1!!$_*3+1}...0}
{ } # Anonymous code block
, ... # Define a sequence
$_ # That starts with the given value
{ } # With each element being
$_%2?? !! # Is the previous element odd?
$_+>1 # Return the previous element bitshifted right by 1
$_*3+1 # Else the previous element multiplied by 3 plus 1
0 # Until the element is 0
add a comment |
up vote
5
down vote
Haskell, 40 39 bytes
f 0=
f n=n:f(cycle[3*n+1,div n 2]!!n)
Try it online!
Now without the final 0.
add a comment |
up vote
4
down vote
Clean, 53 bytes
import StdEnv
$0=[0]
$n=[n: $if(isOdd n)(n/2)(n*3+1)]
Try it online!
add a comment |
up vote
2
down vote
Python 2, 54 52 44 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
-2 bytes thanks to Mr. Xcoder
There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.
Try it online!
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
1
50 bytes
– Jo King
Nov 4 at 23:25
Though the0
is now optional, so it's shorter to get rid of the secondprint
– Jo King
Nov 5 at 2:43
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
|
show 1 more comment
up vote
2
down vote
Haskell, 76 69 61 56 bytes
I feel like this is way too long. Here l
produces an infinite list of the inverse-collatz sequence, and the anonymous function at the first line just cuts it off at the right place.
Thanks for -5 bytes @ØrjanJohansen!
fst.span(>0).l
l r=r:[last$3*k+1:[div k 2|odd k]|k<-l r]
Try it online!
There are no negative numbers, so(>0)
should suffice. Also there's anodd
function.
– Ørjan Johansen
Nov 5 at 18:25
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
add a comment |
up vote
2
down vote
Rust, 65 64 bytes
|mut n|{while n>0{print!("{} ",n);n=if n&1>0{n>>1}else{n*3+1};}}
Try it online!
add a comment |
up vote
2
down vote
05AB1E, 15 14 bytes
[Ð=_#Èi3*>ë<2÷
-1 byte thanks to @MagicOctopusUrn.
Try it online.
Explanation:
[ # Start an infinite loop
Ð # Duplicate the top value on the stack three times
# (Which will be the (implicit) input in the first iteration)
= # Output it with trailing newline (without popping the value)
_# # If it's exactly 0: stop the infinite loop
Èi # If it's even:
3* # Multiply by 3
> # And add 1
ë # Else:
< # Subtract 1
2÷ # And integer-divide by 2
[Ð=_#Èi3*>ë<2÷
with=
instead ofD,
.
– Magic Octopus Urn
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
add a comment |
up vote
2
down vote
JAEL, 18 bytes
![ؼw>î?èÛ|õÀ
Try it online!
1
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
add a comment |
up vote
1
down vote
Jelly, 9 bytes
:++‘ƊḂ?Ƭ2
Try it online!
add a comment |
up vote
1
down vote
JavaScript (ES6), 31 bytes
f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)
Try it online!
Or 30 bytes in reverse order.
add a comment |
up vote
1
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
add a comment |
up vote
1
down vote
Wolfram Language (Mathematica), 35 bytes
0<Echo@#&�[3#+1-(5#+3)/2#~Mod~2]&
Try it online!
0<Echo@# && ...&
is short-circuit evaluation: it prints the input #
, checks if it's positive, and if so, evaluates ...
. In this case, ...
is #0[3#+1-(5#+3)/2#~Mod~2]
; since #0
(the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2
, which simplifies to 3#+1
when #
is even, and (#-1)/2
when #
is odd.
add a comment |
up vote
1
down vote
Common Lisp, 79 bytes
(defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))
Try it online!
add a comment |
up vote
1
down vote
PowerShell, 53 52 bytes
param($i)for(;$i){$i;$i=(($i*3+1),($i-shr1))[$i%2]}0
Try it Online!
Edit:
-1 byte thanks to @mazzy
you can tryfor(;$i)
insteadwhile($i)
– mazzy
Nov 5 at 12:19
add a comment |
up vote
1
down vote
Emojicode 0.5, 141 bytes
🐖🎅🏿🍇🍮a🐕😀🔡a 10🔁▶️a 0🍇🍊😛🚮a 2 1🍇🍮a➗a 2🍉🍓🍇🍮a➕✖️a 3 1🍉😀🔡a 10🍉🍉
Try it online!
🐖🎅🏿🍇
🍮a🐕 👴 input integer variable 'a'
😀🔡a 10 👴 print input int
🔁▶️a 0🍇 👴 loop while number isn’t 0
🍊😛🚮a 2 1🍇 👴 if number is odd
🍮a➗a 2 👴 divide number by 2
🍉
🍓🍇 👴 else
🍮a➕✖️a 3 1 👴 multiply by 3 and add 1
🍉
😀🔡a 10 👴 print iteration
🍉🍉
add a comment |
up vote
1
down vote
Stax, 11 bytes
₧↑╔¶┘tÇ╣;↑è
Run and debug it
add a comment |
up vote
1
down vote
x86 machine code, 39 bytes
00000000: 9150 6800 0000 00e8 fcff ffff 5958 a901 .Ph.........YX..
00000010: 0000 0074 04d1 e8eb 066a 035a f7e2 4009 ...t.....j.Z..@.
00000020: c075 dec3 2564 20 .u..%d
Assembly:
section .text
global func
extern printf
func: ;the function uses fastcall conventions
xchg eax, ecx ;load function arg into eax
loop:
push eax
push fmt
call printf ;print eax
pop ecx
pop eax
test eax, 1 ;if even zf=1
jz even ;if eax is even jmp to even
odd: ;eax=eax/2
shr eax, 1
jmp skip
even: ;eax=eax*3+1
push 3
pop edx
mul edx
inc eax
skip:
or eax, eax
jne loop ;if eax!=0, keep looping
ret ;return eax
section .data
fmt db '%d '
Try it online!
add a comment |
up vote
1
down vote
MathGolf, 12 bytes
{o_¥¿½É3*)}∟
Try it online!
Explanation
{ Start block of arbitrary length
o Output the number
_ Duplicate
¥ Modulo 2
¿ If-else with the next two blocks. Implicit blocks consist of 1 operator
½ Halve the number to integer (effectively subtracting 1 before)
É Start block of 3 bytes
3*) Multiply by 3 and add 1
}∟ End block and make it do-while-true
add a comment |
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
{say;$_=$_%2?$_/2:3*$_+1 and redo}say 0
add a comment |
up vote
0
down vote
Add++, 38 35 33 bytes
D,f,@:,d3*1+$2/iA2%D
+?
O
Wx,$f>x
Try it online!
How it works
First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,
$$f(x) = begin{cases}
x : text{is even}, & 3x+1 \
x : text{is odd}, & lfloorfrac{x}{2}rfloor
end{cases}$$
When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.
We then duplicate this value (d
), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+
), swap the top two values, then calculate $lfloorfrac{x}{2}rfloor$, leaving $S = [3x+1, lfloorfrac{x}{2}rfloor]$.
Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfrac{x}{2}rfloor, (x : % : 2)]$. Finally, we use D
to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfrac{x}{2}rfloor$.
That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x
. However, x
can be omitted from commands where it is obviously the other argument.
For example, +?
is identical to x+?
, and assigns the input to x
, but as x
is the active variable, it can be omitted. Next, we output x
, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x
. All this does is run $f(x)$, then assign that to x
, updating x
on each iteration of the loop.
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
add a comment |
up vote
0
down vote
Retina 0.8.2, 46 bytes
.+
$*
{*M`1
^(..)+$
$&$&$&$&$&$&111
1(.*)1
$1
Try it online! Explanation:
.+
$*
Convert to unary.
{
Repeat until the value stops changing.
*M`1
Print the value in decimal.
^(..)+$
$&$&$&$&$&$&111
If it is even, multiply by 6 and add 3.
1(.*)1
$1
Subtract 1 and divide by 2.
The trailing newline can be suppressed by adding a ;
before the {
.
add a comment |
up vote
0
down vote
Red, 70 bytes
func[n][print n if n = 0[exit]either odd? n[f n - 1 / 2][f n * 3 + 1]]
Try it online!
add a comment |
up vote
0
down vote
PHP, 51 bytes
<?for(;$n=&$argn;$n=$n%2?$n>>1:$n*3+1)echo"$n "?>0
(trailing newline)
Using -n
to ignore the config file (use default settings) , and -F
to read input (each line of input is passed to the script as $argn
).
Try it online!
add a comment |
up vote
0
down vote
Racket, 75 bytes
(define(f n)(cons n(if(= n 0)'()(if(odd? n)(f(/(- n 1)2))(f(+(* 3 n)1))))))
Try it online!
Equivalent to JRowan's Common Lisp solution.
add a comment |
up vote
0
down vote
C# (.NET Core), 62 bytes
a=>{for(;a>0;a=a%2<1?a*3+1:a/2)Console.Write(a+" ");return a;}
Try it online!
Ungolfed:
a => {
for(; a > 0; // until a equals 0
a = a % 2 < 1 ? // set a depending on if a is odd or even
a * 3 + 1 : // even
a / 2 // odd (minus one unnecessary because of int casting)
)
Console.Write(a + " "); // writes the current a to the console
return a; // writes a to the console (always 0)
}
add a comment |
25 Answers
25
active
oldest
votes
25 Answers
25
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Perl 6, 30 bytes
{$_,{$_%2??$_+>1!!$_*3+1}...0}
Try it online!
Anonymous code block that returns a sequence.
Explanation:
{$_,{$_%2??$_+>1!!$_*3+1}...0}
{ } # Anonymous code block
, ... # Define a sequence
$_ # That starts with the given value
{ } # With each element being
$_%2?? !! # Is the previous element odd?
$_+>1 # Return the previous element bitshifted right by 1
$_*3+1 # Else the previous element multiplied by 3 plus 1
0 # Until the element is 0
add a comment |
up vote
5
down vote
Perl 6, 30 bytes
{$_,{$_%2??$_+>1!!$_*3+1}...0}
Try it online!
Anonymous code block that returns a sequence.
Explanation:
{$_,{$_%2??$_+>1!!$_*3+1}...0}
{ } # Anonymous code block
, ... # Define a sequence
$_ # That starts with the given value
{ } # With each element being
$_%2?? !! # Is the previous element odd?
$_+>1 # Return the previous element bitshifted right by 1
$_*3+1 # Else the previous element multiplied by 3 plus 1
0 # Until the element is 0
add a comment |
up vote
5
down vote
up vote
5
down vote
Perl 6, 30 bytes
{$_,{$_%2??$_+>1!!$_*3+1}...0}
Try it online!
Anonymous code block that returns a sequence.
Explanation:
{$_,{$_%2??$_+>1!!$_*3+1}...0}
{ } # Anonymous code block
, ... # Define a sequence
$_ # That starts with the given value
{ } # With each element being
$_%2?? !! # Is the previous element odd?
$_+>1 # Return the previous element bitshifted right by 1
$_*3+1 # Else the previous element multiplied by 3 plus 1
0 # Until the element is 0
Perl 6, 30 bytes
{$_,{$_%2??$_+>1!!$_*3+1}...0}
Try it online!
Anonymous code block that returns a sequence.
Explanation:
{$_,{$_%2??$_+>1!!$_*3+1}...0}
{ } # Anonymous code block
, ... # Define a sequence
$_ # That starts with the given value
{ } # With each element being
$_%2?? !! # Is the previous element odd?
$_+>1 # Return the previous element bitshifted right by 1
$_*3+1 # Else the previous element multiplied by 3 plus 1
0 # Until the element is 0
answered Nov 4 at 23:23
Jo King
18.7k241101
18.7k241101
add a comment |
add a comment |
up vote
5
down vote
Haskell, 40 39 bytes
f 0=
f n=n:f(cycle[3*n+1,div n 2]!!n)
Try it online!
Now without the final 0.
add a comment |
up vote
5
down vote
Haskell, 40 39 bytes
f 0=
f n=n:f(cycle[3*n+1,div n 2]!!n)
Try it online!
Now without the final 0.
add a comment |
up vote
5
down vote
up vote
5
down vote
Haskell, 40 39 bytes
f 0=
f n=n:f(cycle[3*n+1,div n 2]!!n)
Try it online!
Now without the final 0.
Haskell, 40 39 bytes
f 0=
f n=n:f(cycle[3*n+1,div n 2]!!n)
Try it online!
Now without the final 0.
edited Nov 5 at 13:04
answered Nov 4 at 23:43
Lynn
49k694223
49k694223
add a comment |
add a comment |
up vote
4
down vote
Clean, 53 bytes
import StdEnv
$0=[0]
$n=[n: $if(isOdd n)(n/2)(n*3+1)]
Try it online!
add a comment |
up vote
4
down vote
Clean, 53 bytes
import StdEnv
$0=[0]
$n=[n: $if(isOdd n)(n/2)(n*3+1)]
Try it online!
add a comment |
up vote
4
down vote
up vote
4
down vote
Clean, 53 bytes
import StdEnv
$0=[0]
$n=[n: $if(isOdd n)(n/2)(n*3+1)]
Try it online!
Clean, 53 bytes
import StdEnv
$0=[0]
$n=[n: $if(isOdd n)(n/2)(n*3+1)]
Try it online!
answered Nov 4 at 21:19
Οurous
5,76311031
5,76311031
add a comment |
add a comment |
up vote
2
down vote
Python 2, 54 52 44 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
-2 bytes thanks to Mr. Xcoder
There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.
Try it online!
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
1
50 bytes
– Jo King
Nov 4 at 23:25
Though the0
is now optional, so it's shorter to get rid of the secondprint
– Jo King
Nov 5 at 2:43
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
|
show 1 more comment
up vote
2
down vote
Python 2, 54 52 44 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
-2 bytes thanks to Mr. Xcoder
There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.
Try it online!
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
1
50 bytes
– Jo King
Nov 4 at 23:25
Though the0
is now optional, so it's shorter to get rid of the secondprint
– Jo King
Nov 5 at 2:43
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
Python 2, 54 52 44 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
-2 bytes thanks to Mr. Xcoder
There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.
Try it online!
Python 2, 54 52 44 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
-2 bytes thanks to Mr. Xcoder
There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.
Try it online!
edited Nov 5 at 13:28
answered Nov 4 at 21:29
Quintec
1,125517
1,125517
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
1
50 bytes
– Jo King
Nov 4 at 23:25
Though the0
is now optional, so it's shorter to get rid of the secondprint
– Jo King
Nov 5 at 2:43
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
|
show 1 more comment
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
1
50 bytes
– Jo King
Nov 4 at 23:25
Though the0
is now optional, so it's shorter to get rid of the secondprint
– Jo King
Nov 5 at 2:43
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
-2 bytes
– Mr. Xcoder
Nov 4 at 21:48
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
@Mr.Xcoder Ah, thanks.
– Quintec
Nov 4 at 21:50
1
1
50 bytes
– Jo King
Nov 4 at 23:25
50 bytes
– Jo King
Nov 4 at 23:25
Though the
0
is now optional, so it's shorter to get rid of the second print
– Jo King
Nov 5 at 2:43
Though the
0
is now optional, so it's shorter to get rid of the second print
– Jo King
Nov 5 at 2:43
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
Indeed, now you can do it in 44
– Mr. Xcoder
Nov 5 at 11:16
|
show 1 more comment
up vote
2
down vote
Haskell, 76 69 61 56 bytes
I feel like this is way too long. Here l
produces an infinite list of the inverse-collatz sequence, and the anonymous function at the first line just cuts it off at the right place.
Thanks for -5 bytes @ØrjanJohansen!
fst.span(>0).l
l r=r:[last$3*k+1:[div k 2|odd k]|k<-l r]
Try it online!
There are no negative numbers, so(>0)
should suffice. Also there's anodd
function.
– Ørjan Johansen
Nov 5 at 18:25
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
add a comment |
up vote
2
down vote
Haskell, 76 69 61 56 bytes
I feel like this is way too long. Here l
produces an infinite list of the inverse-collatz sequence, and the anonymous function at the first line just cuts it off at the right place.
Thanks for -5 bytes @ØrjanJohansen!
fst.span(>0).l
l r=r:[last$3*k+1:[div k 2|odd k]|k<-l r]
Try it online!
There are no negative numbers, so(>0)
should suffice. Also there's anodd
function.
– Ørjan Johansen
Nov 5 at 18:25
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
add a comment |
up vote
2
down vote
up vote
2
down vote
Haskell, 76 69 61 56 bytes
I feel like this is way too long. Here l
produces an infinite list of the inverse-collatz sequence, and the anonymous function at the first line just cuts it off at the right place.
Thanks for -5 bytes @ØrjanJohansen!
fst.span(>0).l
l r=r:[last$3*k+1:[div k 2|odd k]|k<-l r]
Try it online!
Haskell, 76 69 61 56 bytes
I feel like this is way too long. Here l
produces an infinite list of the inverse-collatz sequence, and the anonymous function at the first line just cuts it off at the right place.
Thanks for -5 bytes @ØrjanJohansen!
fst.span(>0).l
l r=r:[last$3*k+1:[div k 2|odd k]|k<-l r]
Try it online!
edited Nov 5 at 20:32
answered Nov 4 at 23:31
flawr
26k562181
26k562181
There are no negative numbers, so(>0)
should suffice. Also there's anodd
function.
– Ørjan Johansen
Nov 5 at 18:25
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
add a comment |
There are no negative numbers, so(>0)
should suffice. Also there's anodd
function.
– Ørjan Johansen
Nov 5 at 18:25
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
There are no negative numbers, so
(>0)
should suffice. Also there's an odd
function.– Ørjan Johansen
Nov 5 at 18:25
There are no negative numbers, so
(>0)
should suffice. Also there's an odd
function.– Ørjan Johansen
Nov 5 at 18:25
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
@ØrjanJohansen Thanks a lot!
– flawr
Nov 5 at 20:32
add a comment |
up vote
2
down vote
Rust, 65 64 bytes
|mut n|{while n>0{print!("{} ",n);n=if n&1>0{n>>1}else{n*3+1};}}
Try it online!
add a comment |
up vote
2
down vote
Rust, 65 64 bytes
|mut n|{while n>0{print!("{} ",n);n=if n&1>0{n>>1}else{n*3+1};}}
Try it online!
add a comment |
up vote
2
down vote
up vote
2
down vote
Rust, 65 64 bytes
|mut n|{while n>0{print!("{} ",n);n=if n&1>0{n>>1}else{n*3+1};}}
Try it online!
Rust, 65 64 bytes
|mut n|{while n>0{print!("{} ",n);n=if n&1>0{n>>1}else{n*3+1};}}
Try it online!
edited Nov 6 at 14:05
answered Nov 4 at 20:52
Herman L
3,246428
3,246428
add a comment |
add a comment |
up vote
2
down vote
05AB1E, 15 14 bytes
[Ð=_#Èi3*>ë<2÷
-1 byte thanks to @MagicOctopusUrn.
Try it online.
Explanation:
[ # Start an infinite loop
Ð # Duplicate the top value on the stack three times
# (Which will be the (implicit) input in the first iteration)
= # Output it with trailing newline (without popping the value)
_# # If it's exactly 0: stop the infinite loop
Èi # If it's even:
3* # Multiply by 3
> # And add 1
ë # Else:
< # Subtract 1
2÷ # And integer-divide by 2
[Ð=_#Èi3*>ë<2÷
with=
instead ofD,
.
– Magic Octopus Urn
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
add a comment |
up vote
2
down vote
05AB1E, 15 14 bytes
[Ð=_#Èi3*>ë<2÷
-1 byte thanks to @MagicOctopusUrn.
Try it online.
Explanation:
[ # Start an infinite loop
Ð # Duplicate the top value on the stack three times
# (Which will be the (implicit) input in the first iteration)
= # Output it with trailing newline (without popping the value)
_# # If it's exactly 0: stop the infinite loop
Èi # If it's even:
3* # Multiply by 3
> # And add 1
ë # Else:
< # Subtract 1
2÷ # And integer-divide by 2
[Ð=_#Èi3*>ë<2÷
with=
instead ofD,
.
– Magic Octopus Urn
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
add a comment |
up vote
2
down vote
up vote
2
down vote
05AB1E, 15 14 bytes
[Ð=_#Èi3*>ë<2÷
-1 byte thanks to @MagicOctopusUrn.
Try it online.
Explanation:
[ # Start an infinite loop
Ð # Duplicate the top value on the stack three times
# (Which will be the (implicit) input in the first iteration)
= # Output it with trailing newline (without popping the value)
_# # If it's exactly 0: stop the infinite loop
Èi # If it's even:
3* # Multiply by 3
> # And add 1
ë # Else:
< # Subtract 1
2÷ # And integer-divide by 2
05AB1E, 15 14 bytes
[Ð=_#Èi3*>ë<2÷
-1 byte thanks to @MagicOctopusUrn.
Try it online.
Explanation:
[ # Start an infinite loop
Ð # Duplicate the top value on the stack three times
# (Which will be the (implicit) input in the first iteration)
= # Output it with trailing newline (without popping the value)
_# # If it's exactly 0: stop the infinite loop
Èi # If it's even:
3* # Multiply by 3
> # And add 1
ë # Else:
< # Subtract 1
2÷ # And integer-divide by 2
edited 2 days ago
answered Nov 5 at 9:02
Kevin Cruijssen
33.2k554176
33.2k554176
[Ð=_#Èi3*>ë<2÷
with=
instead ofD,
.
– Magic Octopus Urn
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
add a comment |
[Ð=_#Èi3*>ë<2÷
with=
instead ofD,
.
– Magic Octopus Urn
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
[Ð=_#Èi3*>ë<2÷
with =
instead of D,
.– Magic Octopus Urn
2 days ago
[Ð=_#Èi3*>ë<2÷
with =
instead of D,
.– Magic Octopus Urn
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
@MagicOctopusUrn Ah, that was pretty bad to forgot.. Thanks! :)
– Kevin Cruijssen
2 days ago
add a comment |
up vote
2
down vote
JAEL, 18 bytes
![ؼw>î?èÛ|õÀ
Try it online!
1
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
add a comment |
up vote
2
down vote
JAEL, 18 bytes
![ؼw>î?èÛ|õÀ
Try it online!
1
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
add a comment |
up vote
2
down vote
up vote
2
down vote
JAEL, 18 bytes
![ؼw>î?èÛ|õÀ
Try it online!
JAEL, 18 bytes
![ؼw>î?èÛ|õÀ
Try it online!
edited 19 hours ago
answered Nov 4 at 21:13
Eduardo Hoefel
367112
367112
1
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
add a comment |
1
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
1
1
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Your permalink doesn't seem to be working. The program just prints the input and halts.
– Dennis♦
19 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
Yes, you're right. I'll ask "them" to pull the latest version :P
– Eduardo Hoefel
7 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
I've added JAEL to the list of golfing languages. Please let me know if I got any information wrong :-)
– ETHproductions
3 hours ago
add a comment |
up vote
1
down vote
Jelly, 9 bytes
:++‘ƊḂ?Ƭ2
Try it online!
add a comment |
up vote
1
down vote
Jelly, 9 bytes
:++‘ƊḂ?Ƭ2
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Jelly, 9 bytes
:++‘ƊḂ?Ƭ2
Try it online!
Jelly, 9 bytes
:++‘ƊḂ?Ƭ2
Try it online!
answered Nov 4 at 20:41
Erik the Outgolfer
30.2k428100
30.2k428100
add a comment |
add a comment |
up vote
1
down vote
JavaScript (ES6), 31 bytes
f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)
Try it online!
Or 30 bytes in reverse order.
add a comment |
up vote
1
down vote
JavaScript (ES6), 31 bytes
f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)
Try it online!
Or 30 bytes in reverse order.
add a comment |
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 31 bytes
f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)
Try it online!
Or 30 bytes in reverse order.
JavaScript (ES6), 31 bytes
f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)
Try it online!
Or 30 bytes in reverse order.
edited Nov 4 at 20:47
answered Nov 4 at 20:34
Arnauld
68k584288
68k584288
add a comment |
add a comment |
up vote
1
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
add a comment |
up vote
1
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
add a comment |
up vote
1
down vote
up vote
1
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
answered Nov 4 at 20:53
Mr. Xcoder
31.2k758197
31.2k758197
add a comment |
add a comment |
up vote
1
down vote
Wolfram Language (Mathematica), 35 bytes
0<Echo@#&�[3#+1-(5#+3)/2#~Mod~2]&
Try it online!
0<Echo@# && ...&
is short-circuit evaluation: it prints the input #
, checks if it's positive, and if so, evaluates ...
. In this case, ...
is #0[3#+1-(5#+3)/2#~Mod~2]
; since #0
(the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2
, which simplifies to 3#+1
when #
is even, and (#-1)/2
when #
is odd.
add a comment |
up vote
1
down vote
Wolfram Language (Mathematica), 35 bytes
0<Echo@#&�[3#+1-(5#+3)/2#~Mod~2]&
Try it online!
0<Echo@# && ...&
is short-circuit evaluation: it prints the input #
, checks if it's positive, and if so, evaluates ...
. In this case, ...
is #0[3#+1-(5#+3)/2#~Mod~2]
; since #0
(the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2
, which simplifies to 3#+1
when #
is even, and (#-1)/2
when #
is odd.
add a comment |
up vote
1
down vote
up vote
1
down vote
Wolfram Language (Mathematica), 35 bytes
0<Echo@#&�[3#+1-(5#+3)/2#~Mod~2]&
Try it online!
0<Echo@# && ...&
is short-circuit evaluation: it prints the input #
, checks if it's positive, and if so, evaluates ...
. In this case, ...
is #0[3#+1-(5#+3)/2#~Mod~2]
; since #0
(the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2
, which simplifies to 3#+1
when #
is even, and (#-1)/2
when #
is odd.
Wolfram Language (Mathematica), 35 bytes
0<Echo@#&�[3#+1-(5#+3)/2#~Mod~2]&
Try it online!
0<Echo@# && ...&
is short-circuit evaluation: it prints the input #
, checks if it's positive, and if so, evaluates ...
. In this case, ...
is #0[3#+1-(5#+3)/2#~Mod~2]
; since #0
(the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2
, which simplifies to 3#+1
when #
is even, and (#-1)/2
when #
is odd.
answered Nov 4 at 21:40
Misha Lavrov
3,861423
3,861423
add a comment |
add a comment |
up vote
1
down vote
Common Lisp, 79 bytes
(defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))
Try it online!
add a comment |
up vote
1
down vote
Common Lisp, 79 bytes
(defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Common Lisp, 79 bytes
(defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))
Try it online!
Common Lisp, 79 bytes
(defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))
Try it online!
answered Nov 4 at 22:00
JRowan
2214
2214
add a comment |
add a comment |
up vote
1
down vote
PowerShell, 53 52 bytes
param($i)for(;$i){$i;$i=(($i*3+1),($i-shr1))[$i%2]}0
Try it Online!
Edit:
-1 byte thanks to @mazzy
you can tryfor(;$i)
insteadwhile($i)
– mazzy
Nov 5 at 12:19
add a comment |
up vote
1
down vote
PowerShell, 53 52 bytes
param($i)for(;$i){$i;$i=(($i*3+1),($i-shr1))[$i%2]}0
Try it Online!
Edit:
-1 byte thanks to @mazzy
you can tryfor(;$i)
insteadwhile($i)
– mazzy
Nov 5 at 12:19
add a comment |
up vote
1
down vote
up vote
1
down vote
PowerShell, 53 52 bytes
param($i)for(;$i){$i;$i=(($i*3+1),($i-shr1))[$i%2]}0
Try it Online!
Edit:
-1 byte thanks to @mazzy
PowerShell, 53 52 bytes
param($i)for(;$i){$i;$i=(($i*3+1),($i-shr1))[$i%2]}0
Try it Online!
Edit:
-1 byte thanks to @mazzy
edited Nov 5 at 12:53
answered Nov 5 at 10:09
J. Bergmann
813
813
you can tryfor(;$i)
insteadwhile($i)
– mazzy
Nov 5 at 12:19
add a comment |
you can tryfor(;$i)
insteadwhile($i)
– mazzy
Nov 5 at 12:19
you can try
for(;$i)
instead while($i)
– mazzy
Nov 5 at 12:19
you can try
for(;$i)
instead while($i)
– mazzy
Nov 5 at 12:19
add a comment |
up vote
1
down vote
Emojicode 0.5, 141 bytes
🐖🎅🏿🍇🍮a🐕😀🔡a 10🔁▶️a 0🍇🍊😛🚮a 2 1🍇🍮a➗a 2🍉🍓🍇🍮a➕✖️a 3 1🍉😀🔡a 10🍉🍉
Try it online!
🐖🎅🏿🍇
🍮a🐕 👴 input integer variable 'a'
😀🔡a 10 👴 print input int
🔁▶️a 0🍇 👴 loop while number isn’t 0
🍊😛🚮a 2 1🍇 👴 if number is odd
🍮a➗a 2 👴 divide number by 2
🍉
🍓🍇 👴 else
🍮a➕✖️a 3 1 👴 multiply by 3 and add 1
🍉
😀🔡a 10 👴 print iteration
🍉🍉
add a comment |
up vote
1
down vote
Emojicode 0.5, 141 bytes
🐖🎅🏿🍇🍮a🐕😀🔡a 10🔁▶️a 0🍇🍊😛🚮a 2 1🍇🍮a➗a 2🍉🍓🍇🍮a➕✖️a 3 1🍉😀🔡a 10🍉🍉
Try it online!
🐖🎅🏿🍇
🍮a🐕 👴 input integer variable 'a'
😀🔡a 10 👴 print input int
🔁▶️a 0🍇 👴 loop while number isn’t 0
🍊😛🚮a 2 1🍇 👴 if number is odd
🍮a➗a 2 👴 divide number by 2
🍉
🍓🍇 👴 else
🍮a➕✖️a 3 1 👴 multiply by 3 and add 1
🍉
😀🔡a 10 👴 print iteration
🍉🍉
add a comment |
up vote
1
down vote
up vote
1
down vote
Emojicode 0.5, 141 bytes
🐖🎅🏿🍇🍮a🐕😀🔡a 10🔁▶️a 0🍇🍊😛🚮a 2 1🍇🍮a➗a 2🍉🍓🍇🍮a➕✖️a 3 1🍉😀🔡a 10🍉🍉
Try it online!
🐖🎅🏿🍇
🍮a🐕 👴 input integer variable 'a'
😀🔡a 10 👴 print input int
🔁▶️a 0🍇 👴 loop while number isn’t 0
🍊😛🚮a 2 1🍇 👴 if number is odd
🍮a➗a 2 👴 divide number by 2
🍉
🍓🍇 👴 else
🍮a➕✖️a 3 1 👴 multiply by 3 and add 1
🍉
😀🔡a 10 👴 print iteration
🍉🍉
Emojicode 0.5, 141 bytes
🐖🎅🏿🍇🍮a🐕😀🔡a 10🔁▶️a 0🍇🍊😛🚮a 2 1🍇🍮a➗a 2🍉🍓🍇🍮a➕✖️a 3 1🍉😀🔡a 10🍉🍉
Try it online!
🐖🎅🏿🍇
🍮a🐕 👴 input integer variable 'a'
😀🔡a 10 👴 print input int
🔁▶️a 0🍇 👴 loop while number isn’t 0
🍊😛🚮a 2 1🍇 👴 if number is odd
🍮a➗a 2 👴 divide number by 2
🍉
🍓🍇 👴 else
🍮a➕✖️a 3 1 👴 multiply by 3 and add 1
🍉
😀🔡a 10 👴 print iteration
🍉🍉
edited Nov 5 at 16:45
answered Nov 5 at 16:39
X1M4L
979516
979516
add a comment |
add a comment |
up vote
1
down vote
Stax, 11 bytes
₧↑╔¶┘tÇ╣;↑è
Run and debug it
add a comment |
up vote
1
down vote
Stax, 11 bytes
₧↑╔¶┘tÇ╣;↑è
Run and debug it
add a comment |
up vote
1
down vote
up vote
1
down vote
Stax, 11 bytes
₧↑╔¶┘tÇ╣;↑è
Run and debug it
Stax, 11 bytes
₧↑╔¶┘tÇ╣;↑è
Run and debug it
answered Nov 5 at 17:51
wastl
2,054424
2,054424
add a comment |
add a comment |
up vote
1
down vote
x86 machine code, 39 bytes
00000000: 9150 6800 0000 00e8 fcff ffff 5958 a901 .Ph.........YX..
00000010: 0000 0074 04d1 e8eb 066a 035a f7e2 4009 ...t.....j.Z..@.
00000020: c075 dec3 2564 20 .u..%d
Assembly:
section .text
global func
extern printf
func: ;the function uses fastcall conventions
xchg eax, ecx ;load function arg into eax
loop:
push eax
push fmt
call printf ;print eax
pop ecx
pop eax
test eax, 1 ;if even zf=1
jz even ;if eax is even jmp to even
odd: ;eax=eax/2
shr eax, 1
jmp skip
even: ;eax=eax*3+1
push 3
pop edx
mul edx
inc eax
skip:
or eax, eax
jne loop ;if eax!=0, keep looping
ret ;return eax
section .data
fmt db '%d '
Try it online!
add a comment |
up vote
1
down vote
x86 machine code, 39 bytes
00000000: 9150 6800 0000 00e8 fcff ffff 5958 a901 .Ph.........YX..
00000010: 0000 0074 04d1 e8eb 066a 035a f7e2 4009 ...t.....j.Z..@.
00000020: c075 dec3 2564 20 .u..%d
Assembly:
section .text
global func
extern printf
func: ;the function uses fastcall conventions
xchg eax, ecx ;load function arg into eax
loop:
push eax
push fmt
call printf ;print eax
pop ecx
pop eax
test eax, 1 ;if even zf=1
jz even ;if eax is even jmp to even
odd: ;eax=eax/2
shr eax, 1
jmp skip
even: ;eax=eax*3+1
push 3
pop edx
mul edx
inc eax
skip:
or eax, eax
jne loop ;if eax!=0, keep looping
ret ;return eax
section .data
fmt db '%d '
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
x86 machine code, 39 bytes
00000000: 9150 6800 0000 00e8 fcff ffff 5958 a901 .Ph.........YX..
00000010: 0000 0074 04d1 e8eb 066a 035a f7e2 4009 ...t.....j.Z..@.
00000020: c075 dec3 2564 20 .u..%d
Assembly:
section .text
global func
extern printf
func: ;the function uses fastcall conventions
xchg eax, ecx ;load function arg into eax
loop:
push eax
push fmt
call printf ;print eax
pop ecx
pop eax
test eax, 1 ;if even zf=1
jz even ;if eax is even jmp to even
odd: ;eax=eax/2
shr eax, 1
jmp skip
even: ;eax=eax*3+1
push 3
pop edx
mul edx
inc eax
skip:
or eax, eax
jne loop ;if eax!=0, keep looping
ret ;return eax
section .data
fmt db '%d '
Try it online!
x86 machine code, 39 bytes
00000000: 9150 6800 0000 00e8 fcff ffff 5958 a901 .Ph.........YX..
00000010: 0000 0074 04d1 e8eb 066a 035a f7e2 4009 ...t.....j.Z..@.
00000020: c075 dec3 2564 20 .u..%d
Assembly:
section .text
global func
extern printf
func: ;the function uses fastcall conventions
xchg eax, ecx ;load function arg into eax
loop:
push eax
push fmt
call printf ;print eax
pop ecx
pop eax
test eax, 1 ;if even zf=1
jz even ;if eax is even jmp to even
odd: ;eax=eax/2
shr eax, 1
jmp skip
even: ;eax=eax*3+1
push 3
pop edx
mul edx
inc eax
skip:
or eax, eax
jne loop ;if eax!=0, keep looping
ret ;return eax
section .data
fmt db '%d '
Try it online!
answered Nov 5 at 23:55
Logern
66546
66546
add a comment |
add a comment |
up vote
1
down vote
MathGolf, 12 bytes
{o_¥¿½É3*)}∟
Try it online!
Explanation
{ Start block of arbitrary length
o Output the number
_ Duplicate
¥ Modulo 2
¿ If-else with the next two blocks. Implicit blocks consist of 1 operator
½ Halve the number to integer (effectively subtracting 1 before)
É Start block of 3 bytes
3*) Multiply by 3 and add 1
}∟ End block and make it do-while-true
add a comment |
up vote
1
down vote
MathGolf, 12 bytes
{o_¥¿½É3*)}∟
Try it online!
Explanation
{ Start block of arbitrary length
o Output the number
_ Duplicate
¥ Modulo 2
¿ If-else with the next two blocks. Implicit blocks consist of 1 operator
½ Halve the number to integer (effectively subtracting 1 before)
É Start block of 3 bytes
3*) Multiply by 3 and add 1
}∟ End block and make it do-while-true
add a comment |
up vote
1
down vote
up vote
1
down vote
MathGolf, 12 bytes
{o_¥¿½É3*)}∟
Try it online!
Explanation
{ Start block of arbitrary length
o Output the number
_ Duplicate
¥ Modulo 2
¿ If-else with the next two blocks. Implicit blocks consist of 1 operator
½ Halve the number to integer (effectively subtracting 1 before)
É Start block of 3 bytes
3*) Multiply by 3 and add 1
}∟ End block and make it do-while-true
MathGolf, 12 bytes
{o_¥¿½É3*)}∟
Try it online!
Explanation
{ Start block of arbitrary length
o Output the number
_ Duplicate
¥ Modulo 2
¿ If-else with the next two blocks. Implicit blocks consist of 1 operator
½ Halve the number to integer (effectively subtracting 1 before)
É Start block of 3 bytes
3*) Multiply by 3 and add 1
}∟ End block and make it do-while-true
answered 10 hours ago
maxb
1,9681923
1,9681923
add a comment |
add a comment |
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
{say;$_=$_%2?$_/2:3*$_+1 and redo}say 0
add a comment |
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
{say;$_=$_%2?$_/2:3*$_+1 and redo}say 0
add a comment |
up vote
0
down vote
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
{say;$_=$_%2?$_/2:3*$_+1 and redo}say 0
perl -Minteger -nlE, 39 bytes
{say;$_=$_%2?$_/2:3*$_+1 and redo}say 0
answered Nov 4 at 21:16
Abigail
39717
39717
add a comment |
add a comment |
up vote
0
down vote
Add++, 38 35 33 bytes
D,f,@:,d3*1+$2/iA2%D
+?
O
Wx,$f>x
Try it online!
How it works
First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,
$$f(x) = begin{cases}
x : text{is even}, & 3x+1 \
x : text{is odd}, & lfloorfrac{x}{2}rfloor
end{cases}$$
When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.
We then duplicate this value (d
), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+
), swap the top two values, then calculate $lfloorfrac{x}{2}rfloor$, leaving $S = [3x+1, lfloorfrac{x}{2}rfloor]$.
Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfrac{x}{2}rfloor, (x : % : 2)]$. Finally, we use D
to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfrac{x}{2}rfloor$.
That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x
. However, x
can be omitted from commands where it is obviously the other argument.
For example, +?
is identical to x+?
, and assigns the input to x
, but as x
is the active variable, it can be omitted. Next, we output x
, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x
. All this does is run $f(x)$, then assign that to x
, updating x
on each iteration of the loop.
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
add a comment |
up vote
0
down vote
Add++, 38 35 33 bytes
D,f,@:,d3*1+$2/iA2%D
+?
O
Wx,$f>x
Try it online!
How it works
First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,
$$f(x) = begin{cases}
x : text{is even}, & 3x+1 \
x : text{is odd}, & lfloorfrac{x}{2}rfloor
end{cases}$$
When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.
We then duplicate this value (d
), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+
), swap the top two values, then calculate $lfloorfrac{x}{2}rfloor$, leaving $S = [3x+1, lfloorfrac{x}{2}rfloor]$.
Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfrac{x}{2}rfloor, (x : % : 2)]$. Finally, we use D
to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfrac{x}{2}rfloor$.
That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x
. However, x
can be omitted from commands where it is obviously the other argument.
For example, +?
is identical to x+?
, and assigns the input to x
, but as x
is the active variable, it can be omitted. Next, we output x
, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x
. All this does is run $f(x)$, then assign that to x
, updating x
on each iteration of the loop.
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
add a comment |
up vote
0
down vote
up vote
0
down vote
Add++, 38 35 33 bytes
D,f,@:,d3*1+$2/iA2%D
+?
O
Wx,$f>x
Try it online!
How it works
First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,
$$f(x) = begin{cases}
x : text{is even}, & 3x+1 \
x : text{is odd}, & lfloorfrac{x}{2}rfloor
end{cases}$$
When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.
We then duplicate this value (d
), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+
), swap the top two values, then calculate $lfloorfrac{x}{2}rfloor$, leaving $S = [3x+1, lfloorfrac{x}{2}rfloor]$.
Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfrac{x}{2}rfloor, (x : % : 2)]$. Finally, we use D
to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfrac{x}{2}rfloor$.
That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x
. However, x
can be omitted from commands where it is obviously the other argument.
For example, +?
is identical to x+?
, and assigns the input to x
, but as x
is the active variable, it can be omitted. Next, we output x
, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x
. All this does is run $f(x)$, then assign that to x
, updating x
on each iteration of the loop.
Add++, 38 35 33 bytes
D,f,@:,d3*1+$2/iA2%D
+?
O
Wx,$f>x
Try it online!
How it works
First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,
$$f(x) = begin{cases}
x : text{is even}, & 3x+1 \
x : text{is odd}, & lfloorfrac{x}{2}rfloor
end{cases}$$
When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.
We then duplicate this value (d
), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+
), swap the top two values, then calculate $lfloorfrac{x}{2}rfloor$, leaving $S = [3x+1, lfloorfrac{x}{2}rfloor]$.
Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfrac{x}{2}rfloor, (x : % : 2)]$. Finally, we use D
to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfrac{x}{2}rfloor$.
That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x
. However, x
can be omitted from commands where it is obviously the other argument.
For example, +?
is identical to x+?
, and assigns the input to x
, but as x
is the active variable, it can be omitted. Next, we output x
, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x
. All this does is run $f(x)$, then assign that to x
, updating x
on each iteration of the loop.
edited Nov 4 at 21:19
answered Nov 4 at 20:48
caird coinheringaahing
7,39032985
7,39032985
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
add a comment |
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
– Eduardo Hoefel
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@EduardoHoefel Break line?
– caird coinheringaahing
Nov 4 at 21:38
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
@cairdcoinheringaahing The newline characters, presumably.
– Lynn
Nov 4 at 22:13
add a comment |
up vote
0
down vote
Retina 0.8.2, 46 bytes
.+
$*
{*M`1
^(..)+$
$&$&$&$&$&$&111
1(.*)1
$1
Try it online! Explanation:
.+
$*
Convert to unary.
{
Repeat until the value stops changing.
*M`1
Print the value in decimal.
^(..)+$
$&$&$&$&$&$&111
If it is even, multiply by 6 and add 3.
1(.*)1
$1
Subtract 1 and divide by 2.
The trailing newline can be suppressed by adding a ;
before the {
.
add a comment |
up vote
0
down vote
Retina 0.8.2, 46 bytes
.+
$*
{*M`1
^(..)+$
$&$&$&$&$&$&111
1(.*)1
$1
Try it online! Explanation:
.+
$*
Convert to unary.
{
Repeat until the value stops changing.
*M`1
Print the value in decimal.
^(..)+$
$&$&$&$&$&$&111
If it is even, multiply by 6 and add 3.
1(.*)1
$1
Subtract 1 and divide by 2.
The trailing newline can be suppressed by adding a ;
before the {
.
add a comment |
up vote
0
down vote
up vote
0
down vote
Retina 0.8.2, 46 bytes
.+
$*
{*M`1
^(..)+$
$&$&$&$&$&$&111
1(.*)1
$1
Try it online! Explanation:
.+
$*
Convert to unary.
{
Repeat until the value stops changing.
*M`1
Print the value in decimal.
^(..)+$
$&$&$&$&$&$&111
If it is even, multiply by 6 and add 3.
1(.*)1
$1
Subtract 1 and divide by 2.
The trailing newline can be suppressed by adding a ;
before the {
.
Retina 0.8.2, 46 bytes
.+
$*
{*M`1
^(..)+$
$&$&$&$&$&$&111
1(.*)1
$1
Try it online! Explanation:
.+
$*
Convert to unary.
{
Repeat until the value stops changing.
*M`1
Print the value in decimal.
^(..)+$
$&$&$&$&$&$&111
If it is even, multiply by 6 and add 3.
1(.*)1
$1
Subtract 1 and divide by 2.
The trailing newline can be suppressed by adding a ;
before the {
.
answered Nov 4 at 22:45
Neil
77.4k744174
77.4k744174
add a comment |
add a comment |
up vote
0
down vote
Red, 70 bytes
func[n][print n if n = 0[exit]either odd? n[f n - 1 / 2][f n * 3 + 1]]
Try it online!
add a comment |
up vote
0
down vote
Red, 70 bytes
func[n][print n if n = 0[exit]either odd? n[f n - 1 / 2][f n * 3 + 1]]
Try it online!
add a comment |
up vote
0
down vote
up vote
0
down vote
Red, 70 bytes
func[n][print n if n = 0[exit]either odd? n[f n - 1 / 2][f n * 3 + 1]]
Try it online!
Red, 70 bytes
func[n][print n if n = 0[exit]either odd? n[f n - 1 / 2][f n * 3 + 1]]
Try it online!
answered Nov 5 at 9:29
Galen Ivanov
5,57211031
5,57211031
add a comment |
add a comment |
up vote
0
down vote
PHP, 51 bytes
<?for(;$n=&$argn;$n=$n%2?$n>>1:$n*3+1)echo"$n "?>0
(trailing newline)
Using -n
to ignore the config file (use default settings) , and -F
to read input (each line of input is passed to the script as $argn
).
Try it online!
add a comment |
up vote
0
down vote
PHP, 51 bytes
<?for(;$n=&$argn;$n=$n%2?$n>>1:$n*3+1)echo"$n "?>0
(trailing newline)
Using -n
to ignore the config file (use default settings) , and -F
to read input (each line of input is passed to the script as $argn
).
Try it online!
add a comment |
up vote
0
down vote
up vote
0
down vote
PHP, 51 bytes
<?for(;$n=&$argn;$n=$n%2?$n>>1:$n*3+1)echo"$n "?>0
(trailing newline)
Using -n
to ignore the config file (use default settings) , and -F
to read input (each line of input is passed to the script as $argn
).
Try it online!
PHP, 51 bytes
<?for(;$n=&$argn;$n=$n%2?$n>>1:$n*3+1)echo"$n "?>0
(trailing newline)
Using -n
to ignore the config file (use default settings) , and -F
to read input (each line of input is passed to the script as $argn
).
Try it online!
edited Nov 5 at 9:50
answered Nov 5 at 9:44
primo
28.7k544125
28.7k544125
add a comment |
add a comment |
up vote
0
down vote
Racket, 75 bytes
(define(f n)(cons n(if(= n 0)'()(if(odd? n)(f(/(- n 1)2))(f(+(* 3 n)1))))))
Try it online!
Equivalent to JRowan's Common Lisp solution.
add a comment |
up vote
0
down vote
Racket, 75 bytes
(define(f n)(cons n(if(= n 0)'()(if(odd? n)(f(/(- n 1)2))(f(+(* 3 n)1))))))
Try it online!
Equivalent to JRowan's Common Lisp solution.
add a comment |
up vote
0
down vote
up vote
0
down vote
Racket, 75 bytes
(define(f n)(cons n(if(= n 0)'()(if(odd? n)(f(/(- n 1)2))(f(+(* 3 n)1))))))
Try it online!
Equivalent to JRowan's Common Lisp solution.
Racket, 75 bytes
(define(f n)(cons n(if(= n 0)'()(if(odd? n)(f(/(- n 1)2))(f(+(* 3 n)1))))))
Try it online!
Equivalent to JRowan's Common Lisp solution.
answered Nov 5 at 11:22
Galen Ivanov
5,57211031
5,57211031
add a comment |
add a comment |
up vote
0
down vote
C# (.NET Core), 62 bytes
a=>{for(;a>0;a=a%2<1?a*3+1:a/2)Console.Write(a+" ");return a;}
Try it online!
Ungolfed:
a => {
for(; a > 0; // until a equals 0
a = a % 2 < 1 ? // set a depending on if a is odd or even
a * 3 + 1 : // even
a / 2 // odd (minus one unnecessary because of int casting)
)
Console.Write(a + " "); // writes the current a to the console
return a; // writes a to the console (always 0)
}
add a comment |
up vote
0
down vote
C# (.NET Core), 62 bytes
a=>{for(;a>0;a=a%2<1?a*3+1:a/2)Console.Write(a+" ");return a;}
Try it online!
Ungolfed:
a => {
for(; a > 0; // until a equals 0
a = a % 2 < 1 ? // set a depending on if a is odd or even
a * 3 + 1 : // even
a / 2 // odd (minus one unnecessary because of int casting)
)
Console.Write(a + " "); // writes the current a to the console
return a; // writes a to the console (always 0)
}
add a comment |
up vote
0
down vote
up vote
0
down vote
C# (.NET Core), 62 bytes
a=>{for(;a>0;a=a%2<1?a*3+1:a/2)Console.Write(a+" ");return a;}
Try it online!
Ungolfed:
a => {
for(; a > 0; // until a equals 0
a = a % 2 < 1 ? // set a depending on if a is odd or even
a * 3 + 1 : // even
a / 2 // odd (minus one unnecessary because of int casting)
)
Console.Write(a + " "); // writes the current a to the console
return a; // writes a to the console (always 0)
}
C# (.NET Core), 62 bytes
a=>{for(;a>0;a=a%2<1?a*3+1:a/2)Console.Write(a+" ");return a;}
Try it online!
Ungolfed:
a => {
for(; a > 0; // until a equals 0
a = a % 2 < 1 ? // set a depending on if a is odd or even
a * 3 + 1 : // even
a / 2 // odd (minus one unnecessary because of int casting)
)
Console.Write(a + " "); // writes the current a to the console
return a; // writes a to the console (always 0)
}
answered Nov 5 at 17:09
Meerkat
1917
1917
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175248%2fthe-inverse-collatz-conjecture%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
9
As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
Nov 4 at 20:38
Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
Nov 4 at 21:38
Do we have to print the
0
at the end?– flawr
Nov 4 at 23:01
2
You might want to expand the last two test cases, since they're not that long
– Jo King
Nov 4 at 23:07
2
@JoKing I compressed it because it repeats the output from the other lines. At the point you reach 3, it has the same output of when you start from it. The same applies for 10 or any other number.
– Eduardo Hoefel
Nov 5 at 2:40