The inverse Collatz Conjecture
Clash Royale CLAN TAG#URR8PPP
up vote
2
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
add a comment |Â
up vote
2
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
2
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
1 hour ago
Thank you @cairdcoinheringaahing. I didn't know about this page.
â Eduardo Hoefel
38 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
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 36 mins ago
asked 1 hour ago
Eduardo Hoefel
415
415
2
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
1 hour ago
Thank you @cairdcoinheringaahing. I didn't know about this page.
â Eduardo Hoefel
38 mins ago
add a comment |Â
2
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
1 hour ago
Thank you @cairdcoinheringaahing. I didn't know about this page.
â Eduardo Hoefel
38 mins ago
2
2
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
1 hour ago
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
1 hour ago
Thank you @cairdcoinheringaahing. I didn't know about this page.
â Eduardo Hoefel
38 mins ago
Thank you @cairdcoinheringaahing. I didn't know about this page.
â Eduardo Hoefel
38 mins ago
add a comment |Â
11 Answers
11
active
oldest
votes
up vote
0
down vote
Jelly, 9 bytes
:++âÂÂÃÂá¸Â?ì2
Try it online!
add a comment |Â
up vote
0
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
0
down vote
Rust, 65 bytes
fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);
Try it online!
add a comment |Â
up vote
0
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
add a comment |Â
up vote
0
down vote
JAEL, 12 chars, 24 bytes
![â©î?ûá¸Â|õÃÂ⸵áºÂ
I would post the TIO link but it's not working as expected there.
Right now, the input must be prepended to the code, like:
14![â©î?ûá¸Â|õÃÂ⸵áºÂ
add a comment |Â
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
say;$_=$_%2?$_/2:3*$_+1 and redosay 0
add a comment |Â
up vote
0
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
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) = begincases
x : textis even, & 3x+1 \
x : textis odd, & lfloorfracx2rfloor
endcases$$
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 $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.
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, lfloorfracx2rfloor, (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, $lfloorfracx2rfloor$.
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
39 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
add a comment |Â
up vote
0
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
0
down vote
Python 2, 52 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
print 0
-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
29 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
add a comment |Â
up vote
0
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 |Â
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Jelly, 9 bytes
:++âÂÂÃÂá¸Â?ì2
Try it online!
add a comment |Â
up vote
0
down vote
Jelly, 9 bytes
:++âÂÂÃÂá¸Â?ì2
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Jelly, 9 bytes
:++âÂÂÃÂá¸Â?ì2
Try it online!
Jelly, 9 bytes
:++âÂÂÃÂá¸Â?ì2
Try it online!
answered 1 hour ago
Erik the Outgolfer
30k42899
30k42899
add a comment |Â
add a comment |Â
up vote
0
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
0
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
0
down vote
up vote
0
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 1 hour ago
answered 1 hour ago
Arnauld
67.2k584283
67.2k584283
add a comment |Â
add a comment |Â
up vote
0
down vote
Rust, 65 bytes
fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);
Try it online!
add a comment |Â
up vote
0
down vote
Rust, 65 bytes
fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);
Try it online!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Rust, 65 bytes
fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);
Try it online!
Rust, 65 bytes
fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);
Try it online!
answered 1 hour ago
Herman L
3,226428
3,226428
add a comment |Â
add a comment |Â
up vote
0
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
add a comment |Â
up vote
0
down vote
Pyth, 12 bytes
.u?%N2/N2h*3
Try it here as a test suite!
add a comment |Â
up vote
0
down vote
up vote
0
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 1 hour ago
Mr. Xcoder
31.1k758197
31.1k758197
add a comment |Â
add a comment |Â
up vote
0
down vote
JAEL, 12 chars, 24 bytes
![â©î?ûá¸Â|õÃÂ⸵áºÂ
I would post the TIO link but it's not working as expected there.
Right now, the input must be prepended to the code, like:
14![â©î?ûá¸Â|õÃÂ⸵áºÂ
add a comment |Â
up vote
0
down vote
JAEL, 12 chars, 24 bytes
![â©î?ûá¸Â|õÃÂ⸵áºÂ
I would post the TIO link but it's not working as expected there.
Right now, the input must be prepended to the code, like:
14![â©î?ûá¸Â|õÃÂ⸵áºÂ
add a comment |Â
up vote
0
down vote
up vote
0
down vote
JAEL, 12 chars, 24 bytes
![â©î?ûá¸Â|õÃÂ⸵áºÂ
I would post the TIO link but it's not working as expected there.
Right now, the input must be prepended to the code, like:
14![â©î?ûá¸Â|õÃÂ⸵áºÂ
JAEL, 12 chars, 24 bytes
![â©î?ûá¸Â|õÃÂ⸵áºÂ
I would post the TIO link but it's not working as expected there.
Right now, the input must be prepended to the code, like:
14![â©î?ûá¸Â|õÃÂ⸵áºÂ
answered 1 hour ago
Eduardo Hoefel
415
415
add a comment |Â
add a comment |Â
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
say;$_=$_%2?$_/2:3*$_+1 and redosay 0
add a comment |Â
up vote
0
down vote
perl -Minteger -nlE, 39 bytes
say;$_=$_%2?$_/2:3*$_+1 and redosay 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 redosay 0
perl -Minteger -nlE, 39 bytes
say;$_=$_%2?$_/2:3*$_+1 and redosay 0
answered 1 hour ago
Abigail
37716
37716
add a comment |Â
add a comment |Â
up vote
0
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
0
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
0
down vote
up vote
0
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 58 mins ago
ÃÂurous
5,67311031
5,67311031
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) = begincases
x : textis even, & 3x+1 \
x : textis odd, & lfloorfracx2rfloor
endcases$$
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 $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.
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, lfloorfracx2rfloor, (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, $lfloorfracx2rfloor$.
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
39 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
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) = begincases
x : textis even, & 3x+1 \
x : textis odd, & lfloorfracx2rfloor
endcases$$
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 $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.
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, lfloorfracx2rfloor, (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, $lfloorfracx2rfloor$.
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
39 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
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) = begincases
x : textis even, & 3x+1 \
x : textis odd, & lfloorfracx2rfloor
endcases$$
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 $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.
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, lfloorfracx2rfloor, (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, $lfloorfracx2rfloor$.
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) = begincases
x : textis even, & 3x+1 \
x : textis odd, & lfloorfracx2rfloor
endcases$$
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 $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.
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, lfloorfracx2rfloor, (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, $lfloorfracx2rfloor$.
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 58 mins ago
answered 1 hour ago
caird coinheringaahing
7,38032985
7,38032985
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
39 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
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
39 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
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
39 mins ago
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
39 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@EduardoHoefel Break line?
â caird coinheringaahing
38 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
@cairdcoinheringaahing The newline characters, presumably.
â Lynn
4 mins ago
add a comment |Â
up vote
0
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
0
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
0
down vote
up vote
0
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 37 mins ago
Misha Lavrov
3,771323
3,771323
add a comment |Â
add a comment |Â
up vote
0
down vote
Python 2, 52 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
print 0
-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
29 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
add a comment |Â
up vote
0
down vote
Python 2, 52 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
print 0
-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
29 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python 2, 52 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
print 0
-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, 52 bytes
n=input()
while n:print n;n=(n*3+1,n/2)[n%2]
print 0
-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 27 mins ago
answered 48 mins ago
Quintec
1,065517
1,065517
-2 bytes
â Mr. Xcoder
29 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
add a comment |Â
-2 bytes
â Mr. Xcoder
29 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
-2 bytes
â Mr. Xcoder
29 mins ago
-2 bytes
â Mr. Xcoder
29 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
@Mr.Xcoder Ah, thanks.
â Quintec
27 mins ago
add a comment |Â
up vote
0
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
0
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
0
down vote
up vote
0
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 17 mins ago
JRowan
2014
2014
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
2
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
1 hour ago
Thank you @cairdcoinheringaahing. I didn't know about this page.
â Eduardo Hoefel
38 mins ago