71
\$\begingroup\$

If you like this, consider participating in:


Make 12 snippets/expressions, in the same language, that result in the numbers 0 through 10, and 42 respectively, but without writing any literal numeric, string, or character data.

Build-in data, like PI() and ALPHABET(), are fine, and so are e.g. CJam's U, X, Y, Z, and A constants, and Processing's BLEND, CHORD, CENTER, BREAK, and LINES.

Every snippet must be able to stand on its own, i.e. they may not be interdependent. However, inside a single snippet, you may assign a variable and use it freely, as long as you refer to it directly by name, and not through a string containing its name.

All the snippets must be valid on the submitter’s computer at the time of submission (as reported by SE), but may not rely on unusual local conditions like number of files in a directory, the exact date or time, or specific input from the user.

Examples of valid snippets

3: INT(LOG10(YEAR(TODAY()))) because it remains true in the foreseeable future
4: CUBICROOT(LEN(CHARACTERSET())) because a 256 letter character set is very common
8: SQRT(SYSTEMTYPE()) because 64-bit systems are very common

Examples of invalid snippets

5: LEN(USERNAME()) because most people do not use “Admin” as login :-)
9: LOG10(SYSTEMMEMORY()) because it only works on systems with exactly 1 GB of memory
42: CODE("*") because it contains a string/character literal

The result of each snippet must result in an actual number (value, int, float, etc.) that can be used for further calculations using the same language as the snippet, i.e not a text string representing that number.

Only character based languages allowed.

Score is total byte count of all the 12 snippets combined. Newlines separating the snippets are not counted in.

Note that the above rules may prevent some languages from participating, even if they are Turing complete.

FAQ

Q Can the programs accept any input?
A Yes, but you may not just ask for input and enter the relevant number.

Q Are physical digits (non-data) digits allowed?
A Yes, e.g. LOG10().

Q Do Symbols in Ruby count as literals?
A Yes.

Q Does score include newlines between each snippet?
A No.

Q Is TI-BASIC "character based" enough to be valid?
A Yes.

Q Do false and true count as number literals?
A No, they are acceptable.

Q Can we use a number literal to call a function if that's the only way and the number doesn't influence the output of the function?
A Yes, if that is the normal way to write code in your language.

Q My language assumes there is a [something] at the start of each program/expression. Must I include it, or should my snippets just work if placed in the middle of a program/expression?
A They should just work in the middle of a program/expression.

Q What about regex literals?
A Forbidden, except for languages that only do regexes.

Q Is one piece of code that could print all the specified numbers acceptable?
A No, they have to be separate and mutually independent.

Q May I assume a boilerplate like int main() {}... or equivalent?
A Yes.

Q What output datatypes are allowed?
A Any numeric datatype, like int, float, etc.

Q Do I need to print the result of each snippet?
A No, making the result available for subsequent use is enough.

Q Are pre-set variables allowed?
A Yes, and they become reset (if changed) for every snippet.

Q Are π and e considered number literals?
A No, you may use them.

Q May I return 4 and 2 in different cells for 42?
A No, they must be connected as one number.

Q Bytes or characters?
A Bytes, but you may choose any desired codepage.

Q May constant functions and preset variables like J's 9:, Actually's 9, and Pretzel's 9 be used?
A Yes, if the vocabulary is finite (19 for J, 10 for Actually and Pretzel).

\$\endgroup\$
  • \$\begingroup\$ If 0-9 are not number literals but are pre-populated variables, would they be fair game? \$\endgroup\$ – Cyoce Aug 28 '16 at 7:58
  • \$\begingroup\$ @Cyoce So 10 is {1, 0} and not 2×5? \$\endgroup\$ – Adám Aug 29 '16 at 9:34
  • \$\begingroup\$ no, there is simply another syntax for literals that is not base-10, so 0-9 are not literals. They hold the value of 0-9, but are considered variables \$\endgroup\$ – Cyoce Aug 29 '16 at 15:17
  • \$\begingroup\$ @Cyoce Then it's fine. What language is that? \$\endgroup\$ – Adám Aug 29 '16 at 18:56
  • \$\begingroup\$ pretzel (a language I'm working on). \$\endgroup\$ – Cyoce Aug 29 '16 at 22:47

76 Answers 76

1
\$\begingroup\$

Cardinal, 50 bytes

Instructions, with alternatives if they have the same lengths:

0:  IPs are initialized to 0, otherwise, use instruction 0
1:  +
2:  ++
3:  +++
4:  ++++     or ++=*    or ++=t
5:  +++++    or ++=t+   or ++=*+
6:  +++=*    or ++=**
7:  +++=*+   or ++=tt-
8:  ++=tt
9:  +++=t
10: ++=tt*   or +++=t+
42: +++=*=t*

Explanation:

IPs in Cardinal carry length 2 stacks around. The topmost value is called active, the bottom value is called passive. Both can switch roles.

+ increment active value
- decrement active value
* addition : active=active+passive
t multiplication: active=active*passive
= set passive = active value
\$\endgroup\$
1
\$\begingroup\$

Actually, 32 bytes (non-competing)

╜
╒K
 ⌐
╦L
╦K
╦L⌐
╦Lτ
╠²L
╦L╙
╦L²
kY╤
╒º╦+L

All full programs. Try it online!

Explanation

  • is ln(2)
  • is pi
  • is e
  • L is floor
  • K is ceil
  • τ is double
  • ² is square

For the last one:

╒º╦+L
╒      ln(2)
 º     convert from radian to degree (39.71440802747729)
  ╦+   + pi (42.85600068106709)
    L  floor
\$\endgroup\$
  • \$\begingroup\$ Why non-competing? \$\endgroup\$ – Adám Aug 23 '16 at 7:20
  • 1
    \$\begingroup\$ @Adám Because Actually is released this year? \$\endgroup\$ – Leaky Nun Aug 23 '16 at 7:23
  • \$\begingroup\$ I see. Move along. \$\endgroup\$ – Adám Aug 23 '16 at 7:28
  • \$\begingroup\$ @LeakyNun Actually, Actually seems to be started in november 2015 looking at the github graph. I'm not sure when the 'official' release date was, though, so you could be right. Just wanted to use 'Actually, Actually..` Love that programming language's name. ;P \$\endgroup\$ – Kevin Cruijssen Aug 23 '16 at 7:40
  • \$\begingroup\$ @KevinCruijssen Actually and seriously, Seriously should be the one released in Sep 2015. \$\endgroup\$ – Leaky Nun Aug 23 '16 at 7:41
1
\$\begingroup\$

Jelly, 26 bytes (non-competing)

00: 
01: ‘
02: ‘‘
03: ‘‘‘
04: ‘‘Ḥ
05: ⁵H
06: ⁵H‘
07: ⁴H’
08: ⁴H
09: ⁵’
10: ⁵
42: ⁴Ḥ+⁵

Try it online! (paste each one of the snippets in the 'Code' field.)

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

QBIC, 14 bytes (non-competing)

Thank you @Adám, for clarifying part of the rules. Brought the byecount down to 14.

This one's easy in QBIC, but marked non-competing because (this part of) QBIC was made after the challenge was posted.

a
q
r
s
t
u
v
w
x
y
z
v*w

All lower-case letters are numerical variables in QBIC, which default to 0. Exceptions are the letters q-z, which get initialised to 1-10. 42 is simply 6*7.

\$\endgroup\$
  • 1
    \$\begingroup\$ Save 11 bytes by removing the question-marks, as per the FAQ: Q Do I need to print the result of each snippet? A No, making the result available for subsequent use is enough. \$\endgroup\$ – Adám Oct 26 '16 at 9:24
  • \$\begingroup\$ You can still take them out and include your byte count (14). And you were not going to win anyway, as there is already a 13-byte answer. \$\endgroup\$ – Adám Oct 26 '16 at 9:27
1
\$\begingroup\$

Brain-Flak, 172 143 bytes (non-competing)

(<>)
(())
(()())
(()()())
(()()()())
(()()()()())
((()()()){})
((()()()){}())
((()()()()){})
(((()()())){}{})
((()()()()()){})
(((((()()()){}()){})){}{})

Try it online! (copy-paste yourself)

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

tcl, 376

$argc
incr i
llength [list {} {}]
llength [list {} {} {}]
llength [list {} {} {} {}]
llength [list {} {} {} {} {}]
llength [list {} {} {} {} {} {}]
llength [list {} {} {} {} {} {} {}]
llength [list {} {} {} {} {} {} {} {}]
llength [list {} {} {} {} {} {} {} {} {}]
llength [list {} {} {} {} {} {} {} {} {} {}]
expr [llength [list {} {} {} {} {} {} {}]]*[llength [list {} {} {} {} {} {}]]

To try it, on the first line just prepend puts and in the others wrap them by puts [ before and ] after.

demo

\$\endgroup\$
  • \$\begingroup\$ Can't you use two such things and e.g. multiply them? \$\endgroup\$ – Adám Feb 12 '17 at 10:22
  • \$\begingroup\$ @Adám: I've got your point. Thanks! \$\endgroup\$ – sergiol Feb 12 '17 at 13:07
  • \$\begingroup\$ It would seem to me that puts [...] isn't necessary for further computation. \$\endgroup\$ – Adám Feb 12 '17 at 15:54
  • \$\begingroup\$ @Adám: changed according to your observation. \$\endgroup\$ – sergiol Feb 12 '17 at 16:26
1
\$\begingroup\$

SmileBASIC, 73 71 bytes

#NO 'unknown use (maybe dialog box results?)
#UP 'd-pad up
#CHKZ 'used in SPCHK and BGCHK to get the z coordinate
#TRED 'text color red
#LEFT 'd-pad left
#BQLSF 'biquad low shelf filter
#BQHSF 'biquad high shelf filter
#BQPEQ 'biquad peaking equalizer
#CHKI 'used in SPCHK to check definition number
#TBLUE 'text color blue
#TPURPLE 'text color purple
#BQHSF*#BQPEQ '42
\$\endgroup\$
  • \$\begingroup\$ What does the ? do? \$\endgroup\$ – Adám Feb 11 '17 at 21:31
  • \$\begingroup\$ How about 42: #BQLSF*#BQPEQ? \$\endgroup\$ – Adám Feb 11 '17 at 21:32
  • \$\begingroup\$ ? is a shortcut for PRINT, and I just realized this isn't required. \$\endgroup\$ – 12Me21 Feb 11 '17 at 21:45
1
\$\begingroup\$

Lua 5.3, 146 bytes

Uses a lot of length operators. Version probably doesn't matter but it is dependent on the value of _VERSION so I might as well include it for clarity.

0. #{}
1. #{_G}
2. #{_G}+#{_G}
3. #type(x)
4. #type{}-#{_G}
5. #type{}
6. #type{}+#{_G}
7. #_VERSION
8. #type(load)
9. #type(load)+#{_G}
10. #_VERSION+#type(x)
42. - -(#type{}-#{_G}..#{_G}+#{_G})

Annotated with explainations:

0. #{}                 -- {} is an empty table, # is the length operator, length of empty table is 0.
1. #{_G}               -- {_G} is a table containing the global environment (itself a table), so there is one item.
2. #{_G}+#{_G}         -- 1+1
3. #type(x)            -- x is not set and so defaults to nil, type(nil) returns the string "nil", length of "nil" is 3.
4. #type{}-#{_G}       -- 5-1
5. #type{}             -- type of table is "table", length of "table" is 5, brackets not required when only argument is a table literal.
6. #type{}+#{_G}       -- 5+1
7. #_VERSION           -- _VERSION contains the current version as a string ("Lua 5.3"), of length 7.
8. #type(load)         -- type of 'load' is "function", length of "function" is 8.
9. #type(load)+#{_G}   -- 8+1
10. #_VERSION+#type(x) -- 7+3
42. - -(#type{}-#{_G}..#{_G}+#{_G}) -- concatenation operator implicitly converts numerical operands to strings, resulting in "4".."2"=="42". Double negation converts it to a number.
\$\endgroup\$
1
\$\begingroup\$

Japt, 22 bytes

0:  T       T == 0
1:  v       Implicit input 0; Number.v gives 1 if it's even, 0 otherwise
2:  y       Number.y(other) gives GCD of two numbers; `other` defaults to 2
3:  D-A     A == 10, D == 13; 13-10
4:  Gq      G == 16; 16.sqrt()
5:  Az      Number.z(other) gives this/other rounded towards 0; `other` defaults to 2
6:  Cz      C == 12
7:  Ez      E == 14
8:  Iq      I == 64
9:  AÉ      É is a shortcut for `-1`
10: A       Obviously.
42: H+A     H == 32; 32+10

Try it online: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 42

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

MathGolf, 20 bytes

p   # 0
î   # 1
⌠   # 2
d±  # 3
☻√  # 4
♂½  # 5
B½  # 6
D½  # 7
☻½  # 8
d²  # 9
♂   # 10
J∞  # 42

Try it online.

Explanation:

p   # Print the top of the stack (or 0 if there is no input, nor anything on the stack)
î   # 1-indexed loop index (1 by default)
⌠   # + 2 (which becomes 0+2 by default)
d±  # Push -3, and then pop and push its absolute value
☻√  # Push 16, square-rooted
♂½  # Push 10, halved
B½  # Push 12, halved
D½  # Push 14, halved
F½  # Push 16, halved
d²  # Push -3, squared
♂   # Push 10
J∞  # Push 21, doubled

Of course there any many alternatives for some.. i.e. 6 can alternatively be X√; τi; ╟x; ; ; ; ; ; etc. (Try it online).

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

Sinclair ZX81/Timex TX1000/1500 BASIC

After some discussion with Adám below, you have the following on the ZX81** (0 to 10 inclusive and 42), where explanations may be necessary, see the relevant note in the square brackets:

[1] .
[2] NOT .
(PI+PI)/PI
INT PI
INT PI+NOT .
INT (PI+PI)-NOT .
INT (PI+PI)
INT (PI+PI)+NOT .
INT (PI*PI+COS PI)
LEN STR$ PI
INT (PI*PI)+NOT .
[3] INT (PI**PI+PI+PI)

Notes:

  1. This is a short-hand of 0.0
  2. Not zero is 1, this is two keystrokes (so two bytes), which is shorter then PI/PI (three keystrokes)
  3. ** is to the power of, so it's the integer value of PI to the value of PI plus PI+PI

To see this in action, you can type it in as follows using Direct or Online mode in ZX BASIC:

PRINT .,NOT .,(PI+PI)/PI,INT PI,INT PI+NOT .,INT (PI+PI)-NOT .,INT (PI+PI),INT (PI+PI)+NOT .,INT (PI*PI+ COS PI),LEN STR$ PI,INT (PI*PI)+NOT .,INT (PI**PI+PI+PI)

Here is a more evil listing that I came up with that prints 1 to 10 inclusive (try typing it into a ZX81 emulator - actually don't):

1 PRINT LEN STR$ USR VAL (STR$ INT (EXP PI+PI)+STR$ (PI-PI)+STR$ (PI/PI+(PI/PI)))
2 PRINT (PI+PI)/PI
3 PRINT INT PI
4 PRINT INT (SGN PI+PI)
5 PRINT INT PI+(PI/PI)
6 PRINT INT (PI+PI)
7 PRINT INT ((PI+PI)+((PI/PI)))
8 PRINT INT (PI+PI+PI-SGN (PI))
9 PRINT LEN STR$ PI
10 PRINT INT (PI*PI)+PI/PI
\$\endgroup\$
  • 1
    \$\begingroup\$ You don't need PRINTs. You use a lot of forbidden strings, no? graphice should probably be with an S. \$\endgroup\$ – Adám Feb 14 '17 at 17:47
  • \$\begingroup\$ Good spot - thanks. I'll correct the typo. I put it as a program to run so that I could see each outputted number, rather than running everything in direct mode for each entry. \$\endgroup\$ – Shaun Bebbers Feb 14 '17 at 17:48
  • \$\begingroup\$ I also used LET A$="...." simply because I wanted to do something different than using PI - if it's not allowed then my bad. \$\endgroup\$ – Shaun Bebbers Feb 14 '17 at 17:51
  • 1
    \$\begingroup\$ This is code-golf. You've got to strip it down to the very bare minimum, (ab)using quirks. Clarity is not a consideration ;-) \$\endgroup\$ – Adám Feb 15 '17 at 10:27
  • 1
    \$\begingroup\$ It is common to include code-golf solutions twice in answer posts. First the actual golfed submission, then an "un-golfed" one for clarity. Also, you're missing 0, and 1 should be PI/PI, no? May I edit your post? \$\endgroup\$ – Adám Feb 15 '17 at 10:30
0
\$\begingroup\$

C, 78 bytes

Quite a handful of undefined behaviours.

The implementation is gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2), using this online C sandbox.

a-a                //1-1
a                  //1
a+a                //1+1
a+a+a              //1+1+1
++a*a              //2*2
++a+a++            //3+2
++a*a++            //3*2
a+++a+a+a          //1+2+2+2
++a<<a             //2<<2
++a*++a            //3*3
a+++a*++a          //1+3*3
(a=++a*a++)*a+a    //(a=3*2)*a+a

a defaults to 1 being the first argument of main.

Boilerplate example for the last one:

#include <stdio.h>
int main(int a)
{
    printf("%d\n",(a=++a*a++)*a+a);
    return 0;
}

without undefined behaviors, 88 bytes

a-a              //1-1
a                //1
a+a              //1+1
a+a+a            //1+1+1
a<<a+a           //1<<2
a<<a+a|a         //(1<<2)|1
a+a+a<<a         //3<<1
a+a+a<<a|a       //(3<<1)|1
a+a<<a+a         //2<<2
a+a<<a+a|a       //(2<<2)|1
a++;a<<a|a       //(2<<2)|2
a=a+a+a<<a;a*a+a //a=3<<1;a*a+a
\$\endgroup\$
  • \$\begingroup\$ Why wouldn't they be valid? \$\endgroup\$ – Adám Aug 22 '16 at 18:51
  • \$\begingroup\$ @Adám Because a "snippet" is hard to define? \$\endgroup\$ – Leaky Nun Aug 22 '16 at 19:21
  • \$\begingroup\$ That was meant as a relaxation of rules, not a stringency. I don't know C, but can't you remove main(a){ and ;}? \$\endgroup\$ – Adám Aug 22 '16 at 20:03
  • \$\begingroup\$ @Adám I'm not sure. \$\endgroup\$ – Leaky Nun Aug 22 '16 at 20:09
  • \$\begingroup\$ @Adám Given that the a defaults to 1 because it is the argument of main and the name does not have to be a, do you think I can remove the boilerplate? For example, main(c){c=++c+c++;++c*c++;} works as well. \$\endgroup\$ – Leaky Nun Aug 22 '16 at 20:31
0
\$\begingroup\$

Ruby, 149 147 bytes

$.
-~$.
-~-~$.
-~-~-~$.
-~-~-~-~$.
-~-~-~-~-~$.
-~-~-~$.>>~$.
-~-~-~-~-~-~-~$.
-~$.>>~-~-~$.
-~$.>>~-~-~$.|-~$.
-~-~-~-~-~$.>>~$.
-~-~-~-~-~$.>>~-~-~$.|-~-~$.

Explanation:

$.is the number of the last line read from the current input file. Since we are not reading any file, this is initialized with 0.

1 through 5 are just the previous number +1, then I can use shifts for multiplication (negative right shift saves 1 byte) and or for addition (can't use +/- because of priority).

So, for example, 42 is 5>>-3|2

\$\endgroup\$
  • \$\begingroup\$ Can you maybe provide an explanation? \$\endgroup\$ – Adám Feb 15 '17 at 12:05
0
\$\begingroup\$

Excel VBA, 59 bytes

Enums from the Excel object available to VBA. n is said to be an uninitialized variable and thus holds the default value of 0 as a Variant.

These snippets may be called to the VBE immediate window by prepending with ?. e.g. ?xlOr.

Further information on each response is commented to the right

00: -n          ''  2 bytes;  Uninitialized var `n` defaults to `0`
01: n^n         ''  3 bytes;  0^0=1
02: xlOr        ''  4 bytes;  Excel.XlAutoFilterOperator.xlOr
03: xlSet       ''  5 bytes;  Excel.XlCubeFieldType.xlSet
04: xlWKS       ''  5 bytes;  Excel.XlFileFormat.xlWKS
05: xlDXF       ''  5 bytes;  Excel.XlPictureConvertorType.xlDXF
06: xlCSV       ''  5 bytes;  Excel.XlFileFormat.xlCSV
07: xlCGM       ''  5 bytes;  Excel.XlPictureConvertorType.xlCGM
08: xlEPS       ''  5 bytes;  Excel.XlPictureConvertorType.xlEPS
09: xlTIF       ''  5 bytes;  Excel.XlPictureConvertorType.xlTIF
10: xlPCX       ''  5 bytes;  Excel.XlPictureConvertorType.xlPCX
42: xlWJ3+xlOr  ''  10 bytes; Excel.XlFileFormat.xlWJ3+Excel.XlAutoFilterOperator.xlOr
\$\endgroup\$
0
\$\begingroup\$

Husk, 42 bytes

With string and character literals forbidden as well as numbers, all Husk has to work with are sequence builtins: N (all natural numbers), ø (empty list), and the İ family. (And functions which never get evaluated, on account of being wrapped, but that's a bit far from useful.)

Lø        Length of the empty list.
¬ø        Negation of the empty list.
←İp       First prime. I wanted to use \. here, but even though there are no digits . is a (deliberate) corner case of numeric literal syntax and not a special constant.
←İπ       First digit of pi.
□←tN      Second natural number, squared.
←İ5       First power of 5. (Power sequences only exist for 2, 3, 5, and 7; the digits are arbitrary labels.)
D←İ3      First power of 3, times 2.
←İ7       First power of 7.
½LΘİ€     Length of the list of Euro denominations with a zero tacked on to the beginning, over 2.
□←İπ      First digit of pi, squared.
←İ⁰       First power of 10.
D→D←İ⁰    First power of 10, times 2, plus 1, times 2.
\$\endgroup\$
0
\$\begingroup\$

Java 8, 300 bytes

My only goal was to get every value using only java.lang.Math and integer casting. Because of that, it's probably not the best answer in terms of golf. I have fun making it, however, and that's what counts :)

If I had more time, I would have gone further and only used the constants Math.PI and Math.E.

(int)(Math.E-Math.E)              // 0
(int)(Math.E/Math.E)              // 1
(int)Math.E                       // 2
(int)Math.PI                      // 3
(int)(Math.E/Math.E+Math.PI)      // 4
(int)(Math.E+Math.E)              // 5
(int)(Math.PI+Math.PI)            // 6
(int)(Math.E+Math.E+(int)Math.E)  // 7
(int)(Math.E+Math.E+Math.E)       // 8
(int)(Math.PI*Math.PI)            // 9
(int)Math.ceil(Math.PI*Math.PI)   // 10
(int)Math.ceil(Math.pow(Math.PI,Math.PI)+Math.E+Math.E) //42

TIO code to evaluate all of these at once

\$\endgroup\$

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.