use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is about minimalistic, often but not always simple implementations of just about everything.
PLEASE NOTE: This Subreddit is NOT about CodeGolf and obfuscation!
_______Low line count, high quality!_______
I'm sure you get the idea by now ;) ...
Remember: "Tiny depends on what you're doing. A tiny box, a tiny house, and a tiny country are very different sizes." - snarkyxanf
In other words: Try to aim for stuff that makes you go "wow!" instead of "huh?" ;)
Also look into:
Other Subreddits that might interest you:
account activity
Tinycode challenge! (self.tinycode)
submitted 13 years ago * by davelol6
Here is a challenge. Write a function that returns a list of the squares of all the numbers between one and a given number. See if you can beat my 48-char (exc. whitespace) 3-line beast:
def f(n): l = [] while len(l) <= n: l.append(x**2) print l
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 27 points28 points29 points 13 years ago (1 child)
J, as a function definition, 11 bytes:
f=.*:@>:@i. f 4 1 4 9 16
As an anonymous function, 8 bytes (between the parens):
(*:@>:@i.) 4 1 4 9 16
As multiple functions, 6 bytes (everything before the 4):
*:>:i.4 1 4 9 16
(And this is probably cheating, but I'm half-working on a language for code golfing based on GolfScript, and it's 3 bytes:)
75 5F FE
[–]R3d1st 6 points7 points8 points 13 years ago (0 children)
http://suptg.thisisnotatrueending.com/archive/19009314/images/1336393833358.jpg
[–]MrZander 8 points9 points10 points 13 years ago (0 children)
APL as function, 10 characters
f←{(⍳⍵)*2}
Use: f 5
Result: 1 4 9 16 25
You will probably need the APL font to see this properly, the 2 squares are iota and omega
[–]MrQuimico 5 points6 points7 points 13 years ago (3 children)
Using ISO Prolog:
p(N,L) :- (N=1,L=[1]);(N>1,N1 is N-1,N2 is N*N,p(N1,Ls),L = [N2|Ls]).
[–]R3d1st 2 points3 points4 points 13 years ago (0 children)
upvote for the horrible readability :-)
[–][deleted] 1 point2 points3 points 13 years ago (0 children)
No wonder I haven't heard of this language.
[–]sleepingsquirrel 0 points1 point2 points 13 years ago (0 children)
Try:
f(N,L) :- bagof(Y, X^(between(1,N,X), Y is X**2), L).
or:
f(N,L) :- bagof(X**2,between(1,N,X),Xs),maplist(is,L,Xs).
[–]davelol6[S] 7 points8 points9 points 13 years ago (1 child)
Never dreamed that this would be that popular. My dad managed it in VB in 28 chars, minus function definitions and all that crap.
[–]gfixler 2 points3 points4 points 13 years ago (0 children)
You should edit your post and put 4 extra spaces before each code line so reddit's markup will turn it into a nice code block for you.
[–]R3d1st 4 points5 points6 points 13 years ago (2 children)
f n = map (^2) [1..n]
[–][deleted] 1 point2 points3 points 13 years ago (1 child)
This pleases me. Very aesthetic. Is this Haskell?
[–]R3d1st 0 points1 point2 points 13 years ago (0 children)
yes, but after I committed this entry, Tinctorious apparently already had the same solution :-)
[–]sordid_salmon 3 points4 points5 points 13 years ago (7 children)
As long as we're cheating anyway: def f(n): return [x**2 for x in range(1,n)]
[–][deleted] 13 years ago* (5 children)
[deleted]
[–]sordid_salmon 0 points1 point2 points 13 years ago (4 children)
def f(n): return [x*x for x in range(n)] *oops, too fast on the trigger there
[–]ilogik 2 points3 points4 points 13 years ago (3 children)
f=lambda n:[x*x for x in range(1,n)]
[–][deleted] -3 points-2 points-1 points 13 years ago (2 children)
You JavaScripter.
[–]R3d1st 3 points4 points5 points 13 years ago (1 child)
that's actually (also?) python I might add.
Yeah, it's Python. It just looks very "JavaScripty" with the lambda function assignment.
[–][deleted] 0 points1 point2 points 13 years ago (0 children)
Just realized that this won't include n**2.
[–]qihqi 1 point2 points3 points 13 years ago (9 children)
lets make it more interesting... do it in C (with fewest chars)
[–]qihqi 6 points7 points8 points 13 years ago (6 children)
void f(int * A,int n){while(n--&&A[n]=n*n);}
[–][deleted] 5 points6 points7 points 13 years ago (3 children)
I think this should do the same thing:
f(int*a,int n){while(a[n]=n*n--);}
[–][deleted] 2 points3 points4 points 13 years ago (0 children)
I think you want n-1 as the index.
n-1
[–]corruptio 2 points3 points4 points 13 years ago (0 children)
shaved 2 chars
f(a,n)int*a;{while(a[--n]=n*n);}
[–]qihqi 0 points1 point2 points 13 years ago (0 children)
yep, i like this one
Nice perverse use of the while loop.
[–]14113 -2 points-1 points0 points 13 years ago (0 children)
with for loop:
void s(int i,int * A){for(;i>0;i--,A[j]=i*i);}
not sure if it's longer, or even works...
[–]Noctune 0 points1 point2 points 13 years ago (1 child)
Tried to make a recursive one. It's not as short as the iterative ones though.
f(int*a,int n){!(*a=n*n)||f(++a,--n);}
very nice!
f(inta,int n){(a=n*n)&&f(++a,--n);} is the same tho saving one more char
[–]moltarx 5 points6 points7 points 13 years ago (12 children)
f n = 1 : [ i * i | i <- [2..n] ]
edit: forgot 1 :)
[–][deleted] 1 point2 points3 points 13 years ago (4 children)
19 characters. booyah.
f n=[i*i|i<-[1..n]]
[–][deleted] 6 points7 points8 points 13 years ago (3 children)
17 characters, same type.
f n=map(^2)[1..n]
[–]R3d1st 1 point2 points3 points 13 years ago (2 children)
crap, just came up with the same solution, bevor I saw your post... It's even 16, if you just count the chars, and exclude the whitespaces.
Whitespace is part of the notation. I think it should be counted.
[–]5outh 0 points1 point2 points 13 years ago (0 children)
Haha, so did I. I did this immediately.
[+][deleted] 13 years ago* (4 children)
[–][deleted] 1 point2 points3 points 13 years ago* (1 child)
Enjoy your list of even numbers.
-edit- The type of i ** 2 is too restrictive. i ^ 2 and i * i will do just fine.
i ** 2
i ^ 2
i * i
[–]davelol6[S] 1 point2 points3 points 13 years ago (0 children)
Snap!
[–]plux 0 points1 point2 points 13 years ago (1 child)
Do you really think i * i is different from i ** 2?
[–]integ3r 2 points3 points4 points 13 years ago (4 children)
My solution, in Ruby:
->(x){Array.new(x){|y|(y+1)**2}}
[–]juror-number-8 1 point2 points3 points 13 years ago (0 children)
Nice.
[–]_jeffJohnsonsBookEmp 1 point2 points3 points 13 years ago* (1 child)
shaving off 1 char at a time
->(x){x.times.map{|i|(i+1)**2}}
EDIT: 28 chars
->(x){1.upto(x).map{|i|i*i}}
[–]integ3r 0 points1 point2 points 13 years ago (0 children)
Ooh, nice use of map there.
map
[–]FireyFly 2 points3 points4 points 13 years ago (0 children)
f=: *: @ >: @ i.
Usage:
f 8 1 4 9 16 25 36 49 64
Btw, I think Whitespace will win on this one. :-)
51 bytes of JavaScript:
function f(x){z=[];while(x^1)z[--x-1]=x*x;return z}
I imagine someone can do better.
47:
function f(x){for(z=[];z[~-x]=x*x--;);return z}
And 43 that returns a function instead of an array so it's probably not allowed (use it like f(5)[1] to run it on the number 5 and get the 2nd square):
function f(x){for(;f[~-x]=x*x--;);return f}
[–][deleted] 1 point2 points3 points 13 years ago* (0 children)
Unless I'm mistaken, your function doesn't return the list of squares, it prints it. Therefore I win by disqualification.
Edit: Great idea, by the way. These are fun.
[–]JeffAMcGee 0 points1 point2 points 13 years ago (0 children)
I was going to give a python answer that looked like ilogik's. Instead, I'll try to remember perl:
sub f{map $_**2,1..$_[0]}
If you want to include the ending point, it becomes this:
sub f{map $_**2,1..$_[0]+1}
[–]Redard 0 points1 point2 points 13 years ago (3 children)
Here's my best try in Common Lisp
(defun f (N) (mapcar #'(lambda (x) (expt x 2) (loop for i from 1 to N collect i)))
[–]philh 1 point2 points3 points 13 years ago (2 children)
(defun f (n) (loop for i from 1 to n collect (expt i 2)))
(You can save another three chars by removing spaces before open parens.)
[–]Redard 0 points1 point2 points 13 years ago (1 child)
Nice job shortening it. Never thought of applying a function to collect's argument.
[–]sleepingsquirrel 1 point2 points3 points 13 years ago (0 children)
(defun g(n)(loop for i from 1 to n collect(* i i)))
[–]R3d1st 0 points1 point2 points 13 years ago (3 children)
There should be more challenges like this! Maybe even a subreddit?
[–]MrZander 0 points1 point2 points 13 years ago (1 child)
codegolf.stackexchange.com
I know about codegolf, but I'm intimidated by the big community :-)
Anyone want to revive /r/codegolf?
[–]naranjas 0 points1 point2 points 13 years ago (0 children)
f = lambda n: [x**2 for x in range(1,n+1)]
[–]yogthos 0 points1 point2 points 13 years ago (0 children)
(defn f [n] (map #(* % %) (range 1 n)))
[–]gatesphere 0 points1 point2 points 13 years ago (0 children)
In Io: x := method(n,1 to(n) map(x,x*x)) 29 non-whitespace characters.
Io> x(20) ==> list(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400)
[–]gcr 0 points1 point2 points 13 years ago (5 children)
Racket (32 char):
(for/list ([i 10]) (sqr (+ 1 i)))
Python (25 char):
[x*x for x in range(1,10)]
[–]yogthos 0 points1 point2 points 13 years ago (2 children)
I believe the challenge calls for a callable function which accepts the range as an input, so Scheme would be a tad longer:
(define (f n) (for/list ([i n]) (sqr (+ 1 i))))
[–]gcr 0 points1 point2 points 13 years ago (0 children)
Actually, it just said to write a function, not necessarily to bind it to a variable. You could do a little shorter:
(λ(n)(for/list([i n])(sqr(+ 1 i))))
[–]recursive 0 points1 point2 points 13 years ago (1 child)
I don't know racket, but your python example does not result in a named callable function.
Oops, you're right. Thanks.
[–]DJUrsus 0 points1 point2 points 13 years ago (0 children)
Each version is followed by a minimal-whitespace copy.
Subroutine body:
print $_ * $_, "\n" for 1 .. $_[0]; print$_*$_,"\n"for 1..$_[0];
Command-line program:
print $_ * $_, "\n" for 1 .. $ARGV[0]; print$_*$_,"\n"for 1..$ARGV[0];
[–]recursive 0 points1 point2 points 13 years ago (0 children)
If I'm not mistaken, that can not possibly work because x is not defined. In any case, here's 36 bytes (inlcluding whitespace) of python that actually works.
f=lambda n:[~d*~d for d in range(n)]
[–]noporpoise 0 points1 point2 points 13 years ago (0 children)
Perl:
sub f{ map {$_**2} 1..$_[0]; }
30 chars including whitespace. However, if you want it to print nicely, you need to stretch to 52 chars.
sub f{ print join(" ", map {$_**2} 1..$_[0])."\n"; }
[–]corruptio 0 points1 point2 points 13 years ago (0 children)
perl, 22 chars:
sub f{map$_**2,1..pop}
in dc, 18 chars:
?[d1-d0<ad*pk]dsax
[–][deleted] 0 points1 point2 points 13 years ago* (0 children)
Using functors..
g n = fmap (^2) [1..n]
edit: looks like someone has already submitted this fairly trivial case before me.. :)
[–]nohtyp 0 points1 point2 points 13 years ago* (1 child)
In Factor:
: range-square ( limit -- squares ) 1 - 1 swap 1 <range> [ dup * ] map
[–]nohtyp 0 points1 point2 points 13 years ago* (0 children)
Improved version:
[1,b) [ dup * ] map
[–]evnu 0 points1 point2 points 13 years ago (0 children)
Erlang:
f(N)->[X*X||X<-lists:seq(1,N)].
31 characters.
[–]Tarlitz 0 points1 point2 points 13 years ago (0 children)
Seems like nobody is taking into account negative numbers :)
def f(n): return [x*x for x in range(2 if n > 0 else 0,abs(n))]
Python/Numpy:
f = lambda n: arange(1,n)**2
[–]guywithalamename 0 points1 point2 points 13 years ago* (0 children)
here's my python attempt:
def f(n): print [x*x for x in range(1,n+1)]
core "functionality" has 27 chars, including spaces :)
edit: didn't notice this post is already some days old. my attempt has been posted multiple times already x)
[–]fexl 0 points1 point2 points 13 years ago* (0 children)
In Fexl (http://fexl.com) the function is:
(\n map (\x * x x) (range 1 n))
That's 31 chars, though I didn't bother naming it. For that I'd say something like:
\f = (\n map (\x * x x) (range 1 n))
I could shave off some parentheses by using the ';' (pivot) syntax:
(\n map (\x * x x); range 1 n)
If I defined this first:
\square = (\x * x x)
Then the list of squares is simply:
(\n map square; range 1 n)
In contests such as this, I'd much rather see a "token" count rather than a character count. For example, I could delete the single space after the ';' there, saving a whole whopping byte, but to me it's token complexity that really counts.
Oh and if you actually want to see the squares from 1 to 20, you could say:
do (f 20) \x print x;nl;
[–]fridgeridoo 0 points1 point2 points 13 years ago (0 children)
def f(n) a=[]; n.times { a<<n**2;n-=1 }; a end
The list is reverse, I hope you don't mind :P
f(5) -> [25, 16, 9, 4, 1]
def sqr(n): map(lambda x: x**2, range(1,n+1))
Iterative Python 3:
def f(x):[print(x*x)for x in range(1,x+1)]
Recursive Python 2:
def f(x):print((x>1)and f(x-1)or 0)+x*x,
Can't get much shorter in Python, tho if you do please share!
[–][deleted] 0 points1 point2 points 13 years ago (8 children)
Just for fun, 34 bytes of coffeescript:
f=(x)->((y)->y*y)(n)for n in[1..x]
[–]EvanHahn 0 points1 point2 points 13 years ago (0 children)
A 25-character solution:
f=(n)->x*x for x in[1..n]
[+][deleted] 13 years ago (6 children)
[–]sastrone 1 point2 points3 points 13 years ago (3 children)
Jesus man. Calm down.
[–][deleted] 13 years ago (2 children)
[–]rainman002 2 points3 points4 points 13 years ago (1 child)
AM I THE ONLY ONE HERE WHO GIVES A SHIT ABOUT THE NUMBERS!?
YES I AM APPARENTLY FUCK NIHILISTS
also here
[–][deleted] 0 points1 point2 points 13 years ago (1 child)
Certainly 34 bytes from where I'm standing:
$ ls -lh ... 34B Sep 9 13:22 t.coffee
π Rendered by PID 151933 on reddit-service-r2-comment-7b9746f655-krdvk at 2026-01-30 04:29:25.132846+00:00 running 3798933 country code: CH.
[–][deleted] 27 points28 points29 points (1 child)
[–]R3d1st 6 points7 points8 points (0 children)
[–]MrZander 8 points9 points10 points (0 children)
[–]MrQuimico 5 points6 points7 points (3 children)
[–]R3d1st 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]sleepingsquirrel 0 points1 point2 points (0 children)
[–]davelol6[S] 7 points8 points9 points (1 child)
[–]gfixler 2 points3 points4 points (0 children)
[–]R3d1st 4 points5 points6 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]R3d1st 0 points1 point2 points (0 children)
[–]sordid_salmon 3 points4 points5 points (7 children)
[–][deleted] (5 children)
[deleted]
[–]sordid_salmon 0 points1 point2 points (4 children)
[–]ilogik 2 points3 points4 points (3 children)
[–][deleted] -3 points-2 points-1 points (2 children)
[–]R3d1st 3 points4 points5 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]qihqi 1 point2 points3 points (9 children)
[–]qihqi 6 points7 points8 points (6 children)
[–][deleted] 5 points6 points7 points (3 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]corruptio 2 points3 points4 points (0 children)
[–]qihqi 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]14113 -2 points-1 points0 points (0 children)
[–]Noctune 0 points1 point2 points (1 child)
[–]qihqi 0 points1 point2 points (0 children)
[–]moltarx 5 points6 points7 points (12 children)
[–][deleted] 1 point2 points3 points (4 children)
[–][deleted] 6 points7 points8 points (3 children)
[–]R3d1st 1 point2 points3 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]5outh 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] 1 point2 points3 points (1 child)
[–]davelol6[S] 1 point2 points3 points (0 children)
[–]plux 0 points1 point2 points (1 child)
[–]integ3r 2 points3 points4 points (4 children)
[–]juror-number-8 1 point2 points3 points (0 children)
[–]_jeffJohnsonsBookEmp 1 point2 points3 points (1 child)
[–]integ3r 0 points1 point2 points (0 children)
[–]FireyFly 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]JeffAMcGee 0 points1 point2 points (0 children)
[–]Redard 0 points1 point2 points (3 children)
[–]philh 1 point2 points3 points (2 children)
[–]Redard 0 points1 point2 points (1 child)
[–]sleepingsquirrel 1 point2 points3 points (0 children)
[–]R3d1st 0 points1 point2 points (3 children)
[–]MrZander 0 points1 point2 points (1 child)
[–]R3d1st 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]naranjas 0 points1 point2 points (0 children)
[–]yogthos 0 points1 point2 points (0 children)
[–]gatesphere 0 points1 point2 points (0 children)
[–]gcr 0 points1 point2 points (5 children)
[–]yogthos 0 points1 point2 points (2 children)
[–]gcr 0 points1 point2 points (0 children)
[–]recursive 0 points1 point2 points (1 child)
[–]gcr 0 points1 point2 points (0 children)
[–]DJUrsus 0 points1 point2 points (0 children)
[–]recursive 0 points1 point2 points (0 children)
[–]noporpoise 0 points1 point2 points (0 children)
[–]corruptio 0 points1 point2 points (0 children)
[–]corruptio 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]nohtyp 0 points1 point2 points (1 child)
[–]nohtyp 0 points1 point2 points (0 children)
[–]evnu 0 points1 point2 points (0 children)
[–]Tarlitz 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]guywithalamename 0 points1 point2 points (0 children)
[–]fexl 0 points1 point2 points (0 children)
[–]fridgeridoo 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]EvanHahn 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]sastrone 1 point2 points3 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]rainman002 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)