6
\$\begingroup\$

Challenge

Predict the distance between the Sun and the nth planet when using any formula that gives the same result as the Titius–Bode law: d=(3*2^n+4)/10.

BUT WAIT... there is one restriction:

  • Your source code can not include any of the Titius–Bode law's digits
  • So, your program can not contain the characters 0, 1, 2, 3, or 4

Input

n a non-negative integer value:

  • For Mercury n is -∞ no need to handle this case.
  • n=0 for Venus
  • n=1 for Earth
  • n=2 for Mars, and so on ...

Output

Distance in astronomical unit between the Sun and the nth planet (decimal with at least one decimal place).

Titius–Bode law

Distance in astronomical unit between the Sun and the nth planet is estimated by d=(3*2^n+4)/10.

Example

0          --> 0.7
1          --> 1.0 or 1
11         --> 614.8

Rules

  • The input and output can be given in any convenient format.
  • No need to handle invalid input values
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an online testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$
  • 5
    \$\begingroup\$ The inputs and results you show do not match the formula. \$\endgroup\$ – Jeff Zeitlin Apr 11 '18 at 13:24
  • 14
    \$\begingroup\$ Hi, I've downvoted this challenge as it's basically a Do X without Y challenge without much room for creative golfing. \$\endgroup\$ – AdmBorkBork Apr 11 '18 at 13:33
  • 5
    \$\begingroup\$ @AdmBorkBork About "Do X without Y", yes. About "creative golfing", I'm not sure. Let's just see how the answer goes. \$\endgroup\$ – user202729 Apr 11 '18 at 13:36
  • 1
    \$\begingroup\$ I originally downvoted this challenge as I agree with AdmBorkBork's statement (and I still do), but have reverted that downvote simply because of the fun I had making my Whispers solution. \$\endgroup\$ – caird coinheringaahing Apr 11 '18 at 19:29
  • 1
    \$\begingroup\$ "The number 3 is cursed. Avoid it." \$\endgroup\$ – Michael Seifert Apr 12 '18 at 19:22

20 Answers 20

10
\$\begingroup\$

05AB1E, 7 bytes

oxOÌÌT/

Try it online!

Explanation

o          # push 2^input
 x         # pop a and push a, 2*a
  O        # sum stack
   ÌÌ      # add 2 twice
     T/    # divide by 10
\$\endgroup\$
10
\$\begingroup\$

C (gcc), 22 bytes

-Df(x)=((6<<x)+8.)/''

Try it online!

\$\endgroup\$
  • 3
    \$\begingroup\$ I recommend to comment that there is a special character between '' Perhaps it is 'x14'? \$\endgroup\$ – chux - Reinstate Monica Apr 11 '18 at 22:35
  • \$\begingroup\$ Looks like that -D is setting type to double? it seems to work on variable declarations too. I've never seen it before, where is it documented, and what other types are available? It's difficult to google and I can't find it on the PPCG tips for golfing in C question. \$\endgroup\$ – Level River St Apr 11 '18 at 23:26
  • \$\begingroup\$ @LevelRiverSt -D looks like a compile time define in OP's "Try it online!" Nothing to do with double. \$\endgroup\$ – chux - Reinstate Monica Apr 12 '18 at 0:43
  • \$\begingroup\$ @chux it seems you are correct. It would seem though, that this code would be more accurately described as a gcc command line option, rather than C. Certainly shorter than #define in the source file. Google found it documented at rapidtables.com/code/linux/gcc/gcc-d.html \$\endgroup\$ – Level River St Apr 12 '18 at 0:56
  • \$\begingroup\$ @LevelRiverSt It's pointed in meta \$\endgroup\$ – l4m2 Apr 12 '18 at 5:25
5
\$\begingroup\$

JavaScript (Node.js), 20 19 bytes

n=>((6<<n)+8)/5/~-5

Try it online!

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

JavaScript (ES6), 20 bytes

Saved 2 bytes thanks to @l4m2

Does not support Mercury.

n=>(5-9-(9-6<<n))/~9

Try it online!


JavaScript (ES7), 27 bytes

Supports Mercury.

n=>(k=5,--k+--k*--k**n)/-~9

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 9**.5 => 9-6 \$\endgroup\$ – l4m2 Apr 11 '18 at 13:52
4
\$\begingroup\$

Java 8, 20 18 bytes

n->((6<<n)+8f)/''

Port of l4m2's C answer, thanks to Kevin Cruijssen. Try it online here.

Java 8, 30 24 bytes

n->(9-5+(9-6<<n))/(5f+5)

My original approach; admittedly, it's not very creative. Try it online here.

Thanks to Kevin Cruijssen for golfing 6 bytes.

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

dc, 18 17 bytes

zzrkr^zz+*8z/+A/p

Try it online!

Assuming n is all that's on the stack, we get the stack depth twice to push 1 and 2 onto the stack. Reverse these and use the 1 to set our precision (k). Reverse so that our stack is now 2 n, exponentiate (^). Use z to get stack depth twice, again pushing 1 and 2 onto the stack. Add them (+) to get 3, multiply (*) by our previous result. Push 8, get the stack depth with z to push 2, divide these (/) to push 4. Add this (+) to the previous result, and divide by 10 (A/). Finally, print.

Knocked off a byte using 8z/ instead of zF+v to make a 4.

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

Haskell, 29 28 27 25 bytes

s=9-7
d n=(6*s^n+8)/5/s/s

Nothing special, I'm afraid. All allowed digits exactly once!

Try it online!

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

Google Sheets, 27 26 25 23 bytes

=((8-5)*(7-5)^A5+9-5)/(5+5)
=(9^.5*(7-5)^A5+9-5)/(5+5)
=(9^.5*(7-5)^A5+9-5)*.7/7
=(7-5)^A5*(.8-.5)+.9-.5

Take input from cell A5.

For a no-digit version (except the A5 is unavoidable),

=(code("")*code("")^a5+code(""))/code("

(there is a trailing newline)

\$\endgroup\$
  • \$\begingroup\$ How is A5 unavoidable? \$\endgroup\$ – l4m2 Apr 30 '18 at 6:14
3
\$\begingroup\$

Whispers v2, 325 bytes

> 8
> 8
> 8
> 8
> 9
> 6
> Input
>> 5-6
>> 5-55
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 8
> 7
>> 9+9
>> 9*7
>> 55-56
>> 57⋅58
> 8
> 8
> 8
> 8
> 8
>> 58+55
>> 59+56
>> 66÷65
>> Output 67

Try it online!

Let me outline the problem with using Whispers:

Whispers is a single-expression, line-based programming language. Each line consists of a single mathematical expression. However, if the line starts with >>, the numbers used have different values to what they would normally express.

When a line begins with > (such as > 8) the value returned is the number shown, so that example would return 8 when referenced. Input returns an evaluated line from STDIN.

However, when the line starts with >>, such as >> 5-6, the numbers are interpreted as line references, i.e. the above line would call line 5, then line 6, then subtract the results.

Let's create a program that half-sticks to the rules first. This program doesn't use 0, 1, 2, 3, or 4 to represent those numbers, but does use them as line references. This 'half' solution is 96 bytes:

> 6
> 9
> Input
>> 2-1
>> 1=1
>> 4-5
>> 6*3
>> 7⋅4
>> 6+6
>> 8+9
>> 2+5
>> 10÷11
>> Output 12

Try it online!

Unfortunately, the challenge disallows the characters 0, 1, 2, 3, and 4 from appearing in the program, irregardless of what they represent. So, to work around this, we have to use line references with only the numbers 5, 6, 7, 8 and 9 in their digits. This has the consequence that we cannot use any lines between 10 and 54, which is what the massive chain of > 8 are. In fact, all lines that represent a forbidden line reference has > 8, just for consistency.

So, let's remove those lines, but pretend they're still there, we just can't see them:

> 9
> 6
> Input
>> 5-6
>> 5-55
> 7
>> 9+9
>> 9*7
>> 55-56
>> 57⋅58
>> 58+55
>> 59+56
>> 66÷65
>> Output 67

Which is a very similar structure to the 'half' solution. Here's how this shortened solution works, with an input 11:

> 9		; Line  5: Yield 9
> 6		; Line  6: Yield 6
> Input		; Line  7: Yield a line of input (11)
>> 5-6		; Line  8: Yield 3 	-	9 - 6
>> 5-55		; Line  9: Yield 2	-	9 - 7
⋮
> 7		; Line 55: Yield 7
>> 9+9		; Line 56: Yield 4	-	2 + 2
>> 9*7		; Line 57: Yield 2048 	-	2 ^ 11
>> 55-56	; Line 58: Yield 3	-	7 - 4
>> 57⋅58	; Line 59: Yield 6144	-	2048 ⋅ 3
⋮
>> 58+55	; Line 65: Yield 10	-	3 + 7
>> 59+56	; Line 66: Yield 6148	-	6144 + 4
>> 66÷65	; Line 67: Yield 614.8	-	6148 ÷ 10
>> Output 67	; Line 68: Output 614.8
\$\endgroup\$
3
\$\begingroup\$

><>, 27 bytes

l$:?!vll-+$:+!
:*lrl<;n,a+l

Try it online!

Some heavy abuse of the stack length command (l) command here. Luckily, most of the numbers needed are in ascending order. Takes input through the -v flag

\$\endgroup\$
  • \$\begingroup\$ Don't you need to add a byte for the flag? \$\endgroup\$ – Hohmannfan Apr 13 '18 at 8:46
  • \$\begingroup\$ Just now saw your other comment. If that's the convention now I will gladly use it. \$\endgroup\$ – Hohmannfan Apr 13 '18 at 9:00
  • \$\begingroup\$ @Hohmannfan For reference, here's the meta question where it was decided that command line flags are now free \$\endgroup\$ – Jo King Apr 13 '18 at 9:38
3
\$\begingroup\$

Retina 0.8.2, 54 42 35 bytes

.+
@@@$&$*_@@@@
+`@_
_@@
M`@
.$
.$&

Try it online! Link includes test cases. Edit: Saved 12 bytes thanks to @Leo. Explanation:

.
@@@$&$*_@@@@

Generate a pseudo-binary number using @ as 1 and _ as *2, having 3 as the first digit and 4 as the last digit, thus representing 3*2^n+4.

+`@_
_@@

Convert to unary.

M`@

Convert to decimal.

.$
.$&

Divide by 10.

\$\endgroup\$
  • \$\begingroup\$ I think you need .+ on the first line, otherwise it won't work on numbers higher than 9. I see a couple of possible golfs: 1) you can save 1 byte by triplicating each @ and then appending four more @ instead of doing it all at once. 2) To divide by 10 you can just convert to decimal and then insert the point before the last digit. Try it online!. (By the way, you could convert it to Retina 1 by changing $*_ to * and M to C) \$\endgroup\$ – Leo Apr 12 '18 at 2:05
  • \$\begingroup\$ @Leo Whoops, I was really overthinking that decimal conversion, thanks! You also inspired me to find an even better way of multiplying by three and adding four. But I hadn't realised we needed to support that many planets; I don't even know of a solar system with that many. \$\endgroup\$ – Neil Apr 12 '18 at 9:51
  • \$\begingroup\$ Yeah, the number of planets is a bit weird, but one of the test cases is 11... Anyway, your 3*2^n+4 algorithm is amazing, nice work! \$\endgroup\$ – Leo Apr 12 '18 at 14:07
2
\$\begingroup\$

Mathics, 34 bytes

d[n_]:=((9-6)*(8-6)^n+(9-5))/(5+5)

Try it here!

My first time golfing in Mathics, so there's probably a bit more optimizing to be done.

\$\endgroup\$
  • \$\begingroup\$ ((9-6)(8-6)^#+(9-5))/(5+5)& \$\endgroup\$ – ngenisis Apr 12 '18 at 4:39
2
\$\begingroup\$

Symbolic Python, 69 56 bytes

___=_==_
__=-~___
_=__**~-_*-~__+___+___**-___
_/=__-~__

Try it online! Implements (3*2^(n-1)+2)/5

How?

# Input is stored in _ initially

___=_==_         # ___ = True (=1)
__=-~___         # __  = -~1 = 1 + 1 = 2
_=__**~-_*-~__+___+___**-___
# _ = 2**~-_ * -~2 + 1 + 1**-1 = 2**(n-1) * 3 + 1 + 1.0 = 3 * 2**(n-1) + 2.0
_/=__-~__        # _   = _ / (2 -~ 2) = _ / (2 + -~2) = _ / (2 + 2+1) = _ / 5 

# Implicit output of _
\$\endgroup\$
  • \$\begingroup\$ Never thought I'd actually see someone submit an answer in this stupid esolang I decided to make! \$\endgroup\$ – FlipTack Dec 14 '18 at 20:16
2
\$\begingroup\$

Stax, 7 bytes

â╬╫▓♠âε

Run and debug it

PackedStax source encoding makes the character restrictions trivial. Unpacked, ungolfed, and commented this is the same program.

|2  2 to the power
3*  multiply by 3
4+  add 4
A/  divide by 10

Run this one

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

Jelly,  14  8 bytes

-3 thanks to miles (use of æ«, bit-shift) ..and -3 more by golfing from there

6æ«+8H÷⁵

Try it online!

How?

6æ«+8H÷⁵ - Link: number, N (including -inf)
6        - literal six
 æ«      - bit-shift = 6*(2^N)
   +8    - add eight = 6*(2^N)+8
     H   - halve = 3*(2^N)+4
       ⁵ - literal ten
      ÷  - divide = (3*(2^N)+4)/10
\$\endgroup\$
  • \$\begingroup\$ Isn't something incredibly boring like “¤‘æ«+“¥‘÷⁵ a valid solution too? \$\endgroup\$ – miles Apr 11 '18 at 22:47
  • \$\begingroup\$ Hadn't thought of bitshift, so probably \$\endgroup\$ – Jonathan Allan Apr 11 '18 at 22:54
  • \$\begingroup\$ ...yep that and more, thanks :) \$\endgroup\$ – Jonathan Allan Apr 11 '18 at 23:32
2
\$\begingroup\$

Python 3, 27 26 bytes

lambda n:((6-9<<n)-~-5)/~9

Try it online!


Python 2, 38 34 31 28 27 bytes

lambda n:((6-9<<n)-9.+5)/~9

Try it online!


Saved:

  • -4 bytes, thanks to Giuseppe
  • -1 byte, thanks to Jonathan Allan
\$\endgroup\$
  • \$\begingroup\$ f(0) gives 0.55 instead of 0.7? \$\endgroup\$ – mdahmoune Apr 11 '18 at 13:46
  • \$\begingroup\$ @mdahmoune Fixed \$\endgroup\$ – TFeld Apr 11 '18 at 13:46
  • \$\begingroup\$ use negative numbers to save a byte: lambda n:((6-9<<n)-~-5)/~9 \$\endgroup\$ – Jonathan Allan Apr 12 '18 at 0:08
2
\$\begingroup\$

Jelly, 14 12 bytes

‘⁺
¢*+Ḥ$Ç⁺÷⁵

Try it online!

About time for an explanation

How it works

‘⁺        - Helper link. Can be called monadically, or niladically.
          - When called niladically, `0` is the left argument.
 ⁺        - Perform the previous command twice:
‘         -   Increment the left argument

¢*+Ḥ$Ç⁺÷⁵ - Main link. 1 argument: n
¢*        - Yield 2n
    $     - Perform the next two commands on 2n:
   Ḥ      -   Halve.           Yields 2n-1
  +       -   Add to 2n.       Yields 3⋅2n
     Ç    - Add 2.             Yields 3⋅2n + 2
      ⁺   - Add 2 again.       Yields 3⋅2n + 4
        ⁵ - Yield 10
       ÷  - Divide by 10.      Yields (3⋅2n + 4)/10
\$\endgroup\$
1
\$\begingroup\$

C# .NET, 18 bytes

n=>((6<<n)+8f)/''

'' is an unprintable ASCII character with value 20.

Port of @l4m2's C answer.

Try it online.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 25 bytes

->n{6-5.6+6%5.7*(9-7)**n}

Try it online!

Output ±ε due to float imprecision.

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

x86 machine code, 21 20 bytes

6a 03                   push   0x3
58                      pop    eax
d3 e0                   shl    eax,cl
04 04                   add    al,0x4
50                      push   eax
db 04 24                fild   DWORD PTR [esp]
6a 0a                   push   0xa
de 74 24 00             fidiv  WORD PTR [esp+0x0]
58                      pop    eax
58                      pop    eax
c3                      ret

Pretty straightforward, except for two little details:

  1. It's enough to add 4 to al - adding it to eax is 1 byte longer
  2. [esp+0x0] - explicit displacement equal to 0 is needed to avoid the forbidden byte 0x34 (ASCII 4) in the instruction's encoding; thanks to l4m2 for the idea!
\$\endgroup\$
  • \$\begingroup\$ 6A 03 58 D3 E0 04 04 50 DB 04 24 6A 0A DE 74 24 00 58 58 C3? \$\endgroup\$ – l4m2 Apr 15 '18 at 14:16
  • \$\begingroup\$ 6A 03 58 D3 E0 04 04 50 89 E7 DB 07 58 6A 0A DE 37 58 C3 with edi changed \$\endgroup\$ – l4m2 Apr 15 '18 at 15:44
  • \$\begingroup\$ Might work with a suitable calling convention; I only know how to work with fastcall though, on MS Windows. \$\endgroup\$ – anatolyg Apr 15 '18 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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