| Bytes | Lang | Time | Link |
|---|---|---|---|
| 046 | AWK | 250328T185718Z | xrs |
| 091 | SAKO | 250322T164921Z | Acrimori |
| 032 | YASEPL | 240507T161825Z | madeforl |
| 017 | Pip | 230212T205038Z | The Thon |
| 011 | Vyxal 3 | 240504T044443Z | DLosc |
| 8867 | Scala 3 | 240504T083939Z | 138 Aspe |
| 076 | Acc!! | 240504T072147Z | Mukundan |
| 016 | Pyt | 240503T224215Z | Kip the |
| 011 | Nekomata | 230827T064918Z | alephalp |
| 012 | Thunno 2 | 230812T172726Z | The Thon |
| 015 | Pyt | 230212T142142Z | Kip the |
| 012 | Vyxal | 230205T145628Z | lesobrod |
| 070 | Wolfram Language Mathematica | 230205T152306Z | lesobrod |
| 030 | JavaScript ES6 | 181104T203427Z | Arnauld |
| 5015 | Nim | 220815T034148Z | Qaziquza |
| 025 | K ngn/k | 230131T104852Z | zoomlogo |
| 075 | Nibbles | 230131T101910Z | xigoi |
| nan | 230128T175843Z | The Thon | |
| 035 | dc | 220811T230713Z | Benji |
| 075 | C gcc | 211214T003428Z | Benji |
| 056 | Factor + math.extras | 210327T020203Z | chunes |
| 061 | R | 181114T162016Z | Sumner18 |
| 074 | Gambit Scheme gsi | 181214T155849Z | sporkl |
| 049 | Dart | 181214T125359Z | Elcan |
| 039 | x86 machine code | 181105T235539Z | user3604 |
| 012 | MathGolf | 181109T124226Z | maxb |
| 018 | JAEL | 181104T211349Z | Eduardo |
| 014 | 05AB1E | 181105T090256Z | Kevin Cr |
| 064 | Rust | 181104T205235Z | Endenite |
| 056 | Haskell | 181104T233128Z | flawr |
| 011 | Stax | 181105T175128Z | wastl |
| 062 | C# .NET Core | 181105T170950Z | Meerkat |
| 141 | Emojicode 0.5 | 181105T163913Z | X1M4L |
| 044 | Python 2 | 181104T212921Z | Quintec |
| 039 | Haskell | 181104T234325Z | lynn |
| 052 | PowerShell | 181105T100908Z | J. Bergm |
| 075 | Racket | 181105T112202Z | Galen Iv |
| 070 | Red | 181105T092905Z | Galen Iv |
| 030 | Perl 6 | 181104T232313Z | Jo King |
| 046 | Retina 0.8.2 | 181104T224545Z | Neil |
| 079 | Common Lisp | 181104T220009Z | JRowan |
| 035 | Wolfram Language Mathematica | 181104T214026Z | Misha La |
| 033 | Add++ | 181104T204829Z | caird co |
| 053 | Clean | 181104T211904Z | Οurous |
| 039 | perl Minteger nlE | 181104T211613Z | user7392 |
| 012 | Pyth | 181104T205349Z | Mr. Xcod |
| 009 | Jelly | 181104T204100Z | Erik the |
SAKO, 98 91 bytes
2)CZYTAJ:N
DRUKUJ(9,0):N
1)N=ENT((N×3+1)/6*MOD(N,2))
DRUKUJ(9,0):N
GDYN=0:2,INACZEJ1
KONIEC
This probably isn't the shortest solution due to the repeated DRUKUJ, but I have no idea how to make it shorter.
Reads from STDIN. For results above 10 digits long prints in E notation.
Old solution, 98 bytes
2)CZYTAJ:N
DRUKUJ(9,0):N
1)D=1-MOD(N,2)
N=(6*D×(D+N-1))/2+D
DRUKUJ(9,0):N
GDYN=0:2,INACZEJ1
KONIEC
Uses the formula
where D = 1 - N mod 2 for calculating the next value in the sequence.
This is a kind of port of my solution for normal Collatz Conjecture here.
YASEPL, 32 bytes
=x'(+`1-<!z$x%]2!-/-/3`2!*3++[-<
explanation:
=x'(+`1-<!z$x%]2!-/-/3`2!*3++[-< packed
=x'(+ get user input (output number) and add it by one
`1 ! +[ while X+1 != 1
- subtract 1 from X
< print X out
!z$x%]2 if X % 2 == 1...
!-/ subtract 1 and divide by 2
-/3`2!*3+ else, multiply by 3 and add 1
-< print last number when finished
Pip, 29 17 bytes
W Paa:%a?HVaa*3+1
-12 thanks to @DLosc
Explanation
W Paa:%a?HVaa*3+1 # Implicit input as a command line argument, `a`
W # Loop while the condition is truthy (!= 0)
Pa # Print `a` and use this as the condition:
a: # Assign the following to `a`:
%a? # If a is even then:
HVa # a//2
a*3+1 # Otherwise, a*3+1
Vyxal 3, 11 bytes
э:ḃ[v½|Tꜝ}Ŀ
Explanation
э:ḃ[v½|Tꜝ}Ŀ
э # Convert the next three elements to a lambda function:
: # Duplicate
ḃ # Parity (1 if odd, 0 if even)
[ | } # Ternary expression based on that value:
v½ # If truthy (1/odd), decrement and halve
Tꜝ # If falsey (0/even), triple and increment
Ŀ # Apply that function to the input until it repeats a value, and return
# the list of unique values visited
💎
Created with the help of Luminespire.
Alternate 11 bytes, port of Mukundan314's Acc!! answer:
λ:Tꜝ6←ḃ*Ṡ}Ŀ
Scala 3, 88 67 bytes
Saved 21 bytes thanks to @Mukundan314
Golfed version. Attempt This Online!
def c(n:Int):List[Int]=if(n<1)List()else n::c(List(3*n+1,n/2)(n%2))
Ungolfed version. Attempt This Online!
object Main {
def main(args: Array[String]): Unit = {
List(1, 2, 3, 10, 14).map(collatz).foreach(println)
}
def collatz(n: Int): List[Int] = {
if (n == 0) List()
else n :: collatz(next(n))
}
def next(n: Int): Int = {
if (n % 2 == 1) n / 2
else 3 * n + 1
}
}
Acc!!, 76 bytes
Count i while N {
i
}
Count i while _ {
Write 49)*(_
Write 9
(_*3+1)/6^(_%2)
Intput and Output are both in unary
Pyt, 16 bytes
`ĐĐ2%?ŕ⁻₂:ŕ3*⁺;ł
` do...
ĐĐ (implicit input if stack is empty) Đuplicate top of stack twice
2% modulo 2
? is top of stack not 0? (Truthy)
ŕ if so, ŕemove the top of the stack
⁻ decrement
₂ divide by ₂
: else:
ŕ ŕemove the top of the stack
3* multiply by 3
⁺ increment
; either way, continue from here
ł ... while top of stack is not 0 (Falsy); implicit print upon exit
Nekomata, 11 bytes
ᶦ{Z:←½$3*→I
ᶦ{Z:←½$3*→I
ᶦ{ Iterate until failure:
Z Check if nonzero
: Duplicate
← Decrement
½ Halve; fails if odd
$ Swap
3* Multiply by 3
→ Increment
I Choose the first value that doesn't fail
Thunno 2, 12 bytes
(ß;DE?3×⁺:⁻½
Explanation
(ß;DE?3×⁺:⁻½ # implicit input
( # while loop:
ß # (condition) print the number without popping
# and test if it's truthy (non-zero)
;D # (body) duplicate the current integer
E? # if it's even:
3× # multiply it by 3
⁺ # and then add one
: # otherwise:
⁻ # decrement it
½ # and then halve it
Pyt, 15 bytes
`ĐĐ2%?ŕ₂:ŕ3*⁺;ł
` ł do... while top of stack is not 0; implicit print upon exiting loop
ĐĐ duplicate top of stack twice (implicit input if empty)
2% modulo 2
?ŕ₂ if top of stack is truthy, then pop from stack and divide next by 2
:ŕ3*⁺ else: pop from stack and multiply next term by 3 and add 1
; end if-then-else statement
Vyxal, 15 12 bytes
Thanks to @Steffan i learned more about Vyxal!
{…:|:∷[2ḭ|T›
Naive approach for beginners:
{…:Ṡ|:₂[3*›|‹2/
Wolfram Language (Mathematica), 70 bytes
NestWhileList[If[EvenQ@#,3#+1,(#-1)/2]&,#,Unequal,All,\[Infinity],-1]&
Longer than previous WL, but with cycles detection.
Nim, 65 50 bytes (-15 due to xigoi)
proc f(n:int)= echo n;if n>0:f [n*3+1,n/%2][n%%2]
Simple recursive solution cum array-based due to xigoi.
Nibbles, 7.5 bytes
`.$?`%$~@+*3@~
`. Iterate while unique
$ starting at input
? if
`% modulo (also save the quotient)
$ the number
~ 2
@ then the quotient
+ else add
*3 multiply by 3
@ the number
~ 1
Thunno d, \$ 19 \log_{256}(96) \approx \$ 15.64 bytes
[zuZK!)2%?1-2,(3*1+
(No ATO link since this needs v1.2.2 and ATO is on v1.2.1)
Explanation
[zuZK!)2%?1-2,(3*1+ # Implicit input
[ # Loop forever:
zu # Quadruplicate the value on the top of the stack
ZK # Print with a trailing newline
!) # If it's 0, break
2%? # If it's odd:
1- # Subtract one
2, # And integer divide by two
( # Else:
3* # Multiply by three
1+ # And add one
# This value is on the top of the stack for the next iteration
# The d flag stops the implicit output at the end
Screenshot
C (gcc), 75 bytes
#include<stdio.h>
int g(int x){printf("%d ",x);x?g(x=x%2?(x-1)/2:3*x+1):0;}
Factor + math.extras, 56 bytes
[ [ .s dup odd? [ 1 - 2/ ] [ 3 * 1 + ] if ] until-zero ]
Explanation:
[ ... ]A quotation. An anonymous function that lives on the data stack until called or used by a combinator.- Assuming a data stack with
3on top when this quotation is called... [ .s dup odd? [ 1 - 2/ ] [ 3 * 1 + ] if ]Push a quotation to the data stack to be used later byuntil-zero. Stack:3 [ .s dup odd? [ 1 - 2/ ] [ 3 * 1 + ] if ]until-zeroCall a quotation repeatedly until its output is0. Stack:3.sNon-destructively print the data stack. Stack:3dupDuplicate TOS (top-of-stack). Stack:3 3odd?Returntif input is odd, elsef. Stack:3 t[ 1 - 2/ ]Push a quotation to be used later byif. Stack:3 t [ 1 - 2/ ][ 3 * 1 + ]Push another quotation to be used later byif. Stack:3 t [ 1 - 2/ ] [ 3 * 1 + ]ifTakes a boolean and two quotations from the data stack. Calls the first quotation if the boolean ist, otherwise calls the second. (Now inside the first quotation...) Stack:31Push1to the data stack. Stack:3 1-Subtract TOS from NOS (next on stack). Stack:22/Integer divide by 2. Like doingx>>1in many languages. Stack:1- Now
until-zerolooks at the data stack and sees TOS is not0, so calls its input quotation...
R, 66 61 bytes
-5 bytes thanks to Robert S. in consolidating ifelse into if and removing brackets, and x!=0 to x>0
print(x<-scan());while(x>0)print(x<-`if`(x%%2,(x-1)/2,x*3+1))
instead of
print(x<-scan());while(x!=0){print(x<-ifelse(x%%2,(x-1)/2,x*3+1))}
Gambit Scheme (gsi), 74 bytes
(define(f n)(if(= 0 n)'()(cons n(f(if(even? n)(+ (* 3 n)1)(/(- n 1)2))))))
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 (NASM syntax):
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 '
MathGolf, 12 bytes
{o_¥¿½É3*)}∟
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
05AB1E, 15 14 bytes
[Ð=_#Èi3*>ë<2÷
-1 byte thanks to @MagicOctopusUrn.
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
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]
C# (.NET Core), 62 bytes
a=>{for(;a>0;a=a%2<1?a*3+1:a/2)Console.Write(a+" ");return a;}
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)
}
Emojicode 0.5, 141 bytes
🐖🎅🏿🍇🍮a🐕😀🔡a 10🔁▶️a 0🍇🍊😛🚮a 2 1🍇🍮a➗a 2🍉🍓🍇🍮a➕✖️a 3 1🍉😀🔡a 10🍉🍉
🐖🎅🏿🍇
🍮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
🍉🍉
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.
PowerShell, 53 52 bytes
param($i)for(;$i){$i;$i=(($i*3+1),($i-shr1))[$i%2]}0
Edit:
-1 byte thanks to @mazzy
Racket, 75 bytes
(define(f n)(cons n(if(= n 0)'()(if(odd? n)(f(/(- n 1)2))(f(+(* 3 n)1))))))
Equivalent to JRowan's Common Lisp solution.
Perl 6, 30 bytes
{$_,{$_%2??$_+>1!!$_*3+1}...0}
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
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 {.
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))))))
Wolfram Language (Mathematica), 35 bytes
0<Echo@#&�[3#+1-(5#+3)/2#~Mod~2]&
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++, 38 35 33 bytes
D,f,@:,d3*1+$2/iA2%D
+?
O
Wx,$f>x
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}, & \lfloor\frac{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 \$\lfloor\frac{x}{2}\rfloor\$, leaving \$S = [3x+1, \lfloor\frac{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, \lfloor\frac{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, \$\lfloor\frac{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.
perl -Minteger -nlE, 39 bytes
{say;$_=$_%2?$_/2:3*$_+1 and redo}say 0
