3
\$\begingroup\$

Write some code...

Write a program that takes no input and outputs an integer n which is a multiple of 3. So n=3k, where k is a positive integer.
Double the last character of your code.
Your code should then output k.

Examples

Here is my program hui#2(21+4)//lm, which outputs 81. Because of that, hui#2(21+4)//lmm should output 27.
or
If p8$w2*8 outputs 9, p8$w2*88 should output 3.

Rules

This is , shortest answer in bytes wins!

\$\endgroup\$
9
  • 3
    \$\begingroup\$ Can it output 9.0 and 3.0? (Taking square roots, so it automatically turns into a float. :P) \$\endgroup\$ Commented Aug 27, 2017 at 1:02
  • 5
    \$\begingroup\$ This is a less interesting version of double the source, double the output. And that wasn't terribly interesting to begin with. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 27, 2017 at 3:26
  • 3
    \$\begingroup\$ I've downvoted this as, even by my standards, this is far too trivial, solvable with basic subtraction or square rooting, as illustrated by a lot of the solutions so far. \$\endgroup\$
    – Shaggy
    Commented Aug 27, 2017 at 8:04
  • 5
    \$\begingroup\$ @BillSteihn You don't understand - this is code golf. The goal is to find the shortest answer, not to be creative. The answer that Shaggy provided is a trivial way of achieving your answer, and will be the shortest way to solve this challenge in most languages. \$\endgroup\$
    – clismique
    Commented Aug 27, 2017 at 9:20
  • 4
    \$\begingroup\$ As @QwerpDerp said, this is code golf; creativity is, for the most part, not conducive to a healthy byte count. \$\endgroup\$
    – Shaggy
    Commented Aug 27, 2017 at 17:09

34 Answers 34

41
\$\begingroup\$

Mathematica, 2 bytes

4!

4! returns 24
4!! returns 8

\$\endgroup\$
1
  • 11
    \$\begingroup\$ This one is really cute :) \$\endgroup\$
    – lynn
    Commented Aug 27, 2017 at 13:21
36
\$\begingroup\$

Many languages, 4 bytes

16-1

Outputs 15.

16-11

Outputs 5.

This is a CW answer for many languages it works for. Edit to add to the list:

  • APL
  • bc
  • Dyvil
  • Haskell
  • I
  • J
  • Japt
  • Javascript REPL
  • Java REPL
  • Julia REPL
  • Mathematica
  • Python REPL
  • R REPL
  • Ruby REPL
  • Scala REPL
  • Swift REPL
  • TI-83/84 Basic
\$\endgroup\$
7
  • \$\begingroup\$ JavaScript (console only). But I'm not sure if it would be valid. \$\endgroup\$
    – user72349
    Commented Aug 27, 2017 at 2:12
  • \$\begingroup\$ @ThePirateBay I think REPL answers are valid. This should work for Python too, in a REPL environment, right? \$\endgroup\$ Commented Aug 27, 2017 at 2:51
  • \$\begingroup\$ @IsmaelMiguel Yes, though they count as separate languages. i added both. \$\endgroup\$
    – xnor
    Commented Aug 27, 2017 at 2:55
  • \$\begingroup\$ I suspect your list is going to get quite long for this one! Works in Japt (although you could replace 16 with a constant to save a byte). \$\endgroup\$
    – Shaggy
    Commented Aug 27, 2017 at 6:50
  • \$\begingroup\$ Sage as well. :) \$\endgroup\$ Commented Aug 27, 2017 at 14:46
15
\$\begingroup\$

Lost, 52, 18 bytes

\(@>>>
\>>%?
\(13$

Try it online!

\(@>>>
\>>%?
\(13$$

Try it online!

Explanation

Lost is a language in which the start location and direction of the pointer are entirely random. Thus making a deterministic program can be tough.

The code we want to execute is either 13$(@ or 13$$(@.

  • 31 pushes 3 and 1 to the stack
  • $ will swap them
  • $ will swap them back if present
  • ( will pop the top element
  • @ will halt and output

We want to insert the $ at the end so we have to split this

\(@
v
\31$

The rest of this is a careful orchestration to make sure that every possible pointer executes this code and only this code.

\$\endgroup\$
10
\$\begingroup\$

Operation Flashpoint scripting language, 19 bytes

a="3";aa="1";hint a

Outputs:

a="3";aa="1";hint aa

Outputs:

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Wow, just wow :D Anyway, can't you just hint a,aa? \$\endgroup\$
    – KeyWeeUsr
    Commented Aug 27, 2017 at 9:54
  • \$\begingroup\$ @KeyWeeUsr You mean to output both at the same time? hint format["%1\n%2", a, aa] would do that (or hint (a+"\n"+aa) because they are strings), but I think outputting them separately illustrates the solution better here. \$\endgroup\$
    – Steadybox
    Commented Aug 27, 2017 at 17:10
  • \$\begingroup\$ The screenshots are always entertaining now, you sniping a tank, eh? \$\endgroup\$ Commented Nov 7, 2017 at 22:57
  • \$\begingroup\$ @MagicOctopusUrn Just observing it and the surroundings with my binoculars. It's way out of range for any anti-tank weapons I'd might be carrying, so I need to get closer, but first I must check that there's no enemy infantry around preventing me from doing so. \$\endgroup\$
    – Steadybox
    Commented Nov 7, 2017 at 23:05
  • \$\begingroup\$ @Steadybox obviously you'd recommend this game, eh? Is it a steam game? \$\endgroup\$ Commented Nov 7, 2017 at 23:12
10
\$\begingroup\$

Jelly, 2 bytes

Try it online!

Outputs 3000. is a numeric literal for 3000.

3ȷȷ outputs 1000: is 3000, which is thrown away and replaced by ȷ, 1000.

This is the only two-byte solution, and it is optimal; I found it by trying every possible program.

\$\endgroup\$
9
\$\begingroup\$

Actually, 2 bytes

Try it online!

Repeating the last character: 5¬¬.

Explanation:

5¬
5   push 5
 ¬  subtract 2

Adding another ¬ causes 2 to be subtracted from 3, yielding 1.

\$\endgroup\$
1
  • \$\begingroup\$ Paradoc version: , 5«« I hope you find this symbol choice more mnemonic — it was supposed to be one of its comparative strengths :) \$\endgroup\$
    – betaveros
    Commented Aug 27, 2017 at 5:44
7
\$\begingroup\$

Python 2, 9 bytes

print 3&3

Try it online! Prints 3.

print 3&33

Try it online! Prints 1.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Also works with 3&7, 6&6, 7&3, 7&6, or 8^1 \$\endgroup\$
    – benzene
    Commented Aug 27, 2017 at 1:14
  • 3
    \$\begingroup\$ Shave off 6 bytes by using REPL \$\endgroup\$
    – boboquack
    Commented Aug 27, 2017 at 8:37
5
\$\begingroup\$

Pyke, 2 bytes

This prints 3 and 6ee prints 1.

6e

Try it here!

6ee

Try it here!

6  - Push 6.
 e - Floor halve.
   - Print Implicitly.

How?

Not much to explain. The whole point of this solution is that:

\$\endgroup\$
5
\$\begingroup\$

CJam, 4 bytes

2_5+

Outputs 27. Try it online!

2_5++

Outputs 9. Try it online!

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

Haskell, 8 bytes

gcd 3 21

Outputs 3, and gcd 3 211 outputs 1.

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

Jelly, 3 bytes

81½

Try it online!

Outputs 9.

81½½

Try it online!

Outputs 3.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Is ½ really square root? I can't figure which is worse, the ½ or Actually's ¬ \$\endgroup\$
    – boboquack
    Commented Aug 27, 2017 at 8:37
4
\$\begingroup\$

05AB1E, 3 bytes

3Xr

Outputs 3.

Try it online!

3Xrr

Outputs 1.

Try it online!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ , maybe? :P \$\endgroup\$ Commented Aug 27, 2017 at 13:54
  • \$\begingroup\$ тg too ;)...? \$\endgroup\$ Commented Feb 8, 2018 at 16:35
4
\$\begingroup\$

Cubix, 6 bytes

Works by changing the path based on cubes with different edge sizes. None of that tricky math stuff. The starting numbers could be also 1 3 or 2 6

39O@W@

Cubified

  3
9 O @ W
  @

Try it online!

Only executes 9O@ which is Push 9, Output as number, Halt.

39O@W@

Cubified

    3 9
    O @
W @ @ . . . . .
. . . . . . . .
    . .
    . .

This now executes W3O@, which is Left shift, Push 3, Output as number, Halt.

Try it online!

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

Mathematica, 13 bytes

PrimeOmega@27

Not the shortest solution but y'know...

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

Polyglot, 05AB1E / 2sable, 3 bytes

This one prints 9. 81tt prints 3.

81t

Try it online! Try it online!

81tt

Try it online! Try it online!

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

Befunge-98, 6 bytes

<q%5b'

Try it online!

Outputs via exit code.

Explanation: On program start the IP is immediately sent leftwards, so it wraps to the right end and executes commands from right to left. So, in this program, it encounters 'b5%, which pushes 98, the value of the character b, and then computes the remainder of 98 when divided by 5, which is 3. Finally, with q, it quits with this exit code.

With the last character doubled, it encounters ''b5%, which pushes the value of the character ' but then immediately pushes b, which is 11. It then computes the remainder of 11 when divided by 5, which is 1, and quits with this exit code.

\$\endgroup\$
2
  • \$\begingroup\$ "It then computes the remainder of 12 when divided by 5, which is 1, and quits with this exit code." - I think you mean 11... \$\endgroup\$
    – Luke
    Commented Aug 27, 2017 at 18:03
  • \$\begingroup\$ You're right, fixed \$\endgroup\$
    – betaveros
    Commented Aug 27, 2017 at 18:04
3
\$\begingroup\$

C, 60 45 bytes

Thanks to @nwellnhof for saving 15 bytes!

#define AA"5");}
#define A"1"AA
main(){puts(A

Outputs 15

#define AA"5");}
#define A"1"AA
main(){puts(AA

Outputs 5

\$\endgroup\$
2
  • \$\begingroup\$ #define AA"5");} #define A"1"AA main(){puts(A \$\endgroup\$
    – nwellnhof
    Commented Aug 27, 2017 at 18:50
  • \$\begingroup\$ @nwellnhof Thanks! \$\endgroup\$
    – Steadybox
    Commented Aug 27, 2017 at 20:57
2
\$\begingroup\$

Pyth, 4 bytes

This prints 9. x8 11 prints 3.

x8 1

Try it online!

x8 11

Try it online!


How?

x8 1  - Full program.

x     - Bitwise XOR.
 8 1  - Of 8 and 1.
      - Implicitly print.
\$\endgroup\$
2
\$\begingroup\$

Befunge-98 (PyFunge), 5 bytes

[q/3g

Try it online!

Outputs via exit code.

Explanation

First, the IP hits the "turn left" instruction ([) twice, which wraps it back around to the back of the code, going right.

The "get" instruction (g) takes the first 2 values and pushes the ASCII value of the character at that x and y position. The first time it is executed, there are only zeros on the stack, meaning we get the value 91. If we don't double the g, we divide 91 by 3, with integer division, truncating it to 30, which is returned.

If we do duplicate the g, it gets the ASCII value at (0, 91), which isn't anywhere near the code. Because the grid is initialized to be filled with spaces, this returns the value 32. if we integer divide by 3, we get 10, one third of 30.

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

Python 2, 16 bytes

a=3;aa=1;print a

outputs 3

a=3;aa=1;print aa

outputs 1

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

V, 5 bytes

é52ñ

Try it online!

Outputs 3

é52ñ

Try it online!

Outputs 1

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

Ly, 6 bytes

3>1<<>

Try it online!

Outputs 3.

With the second character duplicated:

3>1<<>>

Try it online!

Outputs 1.

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

Recursiva, 5 bytes

+16-1

Try it online!

  • Ripped off from Many languages solution, only a byte more since Recursiva is prefix and - sign and subtract operator are the same. This is actually adding 16 and -1.

Other solution:

Recursiva, 10 bytes

Y_+B5A3IH1

Try it online!

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

Jelly, 5 bytes

This one outputs 3. By doubling the last character, you get 1.

HḞ
6Ç

Try it online!

HḞ
6ÇÇ

Try it online!

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

Japt, 3 bytes

Still on the hunt for a 2 byte solution.

#Q is 81 and ¬ is square root, which gives 9. Adding another ¬ at the end gives 3

#Q¬

Try it

#Q¬¬

Try it

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

><>, 6 bytes

",n;c!

Try it online!

",n;c!!

Try it online!

Explanation

",n;c!  : Put each character onto the stack;
          [110, 59, 99, 33]
 ,n;    : Divide the top two stack items, print as a number and end.
          99/33 = 3

",n;c!! : Put each chat onto the stack;
          [110, 59, 99, 33, 33]
 ,n;    : Divide the top two stack items, print as a number and end.
          99/33 = 3
\$\endgroup\$
1
  • \$\begingroup\$ This ports to Runic and doesn't feel worth posting as its own answer. \$\endgroup\$ Commented Feb 9, 2019 at 23:28
1
\$\begingroup\$

Pyt, 2 bytes

9₃

Returns 9/3

Try it online!


9₃₃

Returns (9/3)/3

Try it online!


Alternatively, also 2 bytes:

6₂

This works due to Python 2's integer division

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

Pyth, 5 bytes

-16 1

Try it online!

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

Charcoal, 6 bytes

This one prints 15. The other version prints 5.

I⁻¹⁶¦¹

Try it online!

I⁻¹⁶¦¹¹

Try it online!

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

Neim, 2 bytes

ηᚠ

Outputs 3. Doubled, outputs 1.

Try it online!

Doubled

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.