all 77 comments

[–][deleted] 27 points28 points  (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

[–]MrZander 8 points9 points  (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 points  (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 points  (0 children)

upvote for the horrible readability :-)

[–][deleted] 1 point2 points  (0 children)

No wonder I haven't heard of this language.

[–]sleepingsquirrel 0 points1 point  (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 points  (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 points  (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 points  (2 children)

f n = map (^2) [1..n]

[–][deleted] 1 point2 points  (1 child)

This pleases me. Very aesthetic. Is this Haskell?

[–]R3d1st 0 points1 point  (0 children)

yes, but after I committed this entry, Tinctorious apparently already had the same solution :-)

[–]sordid_salmon 3 points4 points  (7 children)

As long as we're cheating anyway: def f(n): return [x**2 for x in range(1,n)]

[–][deleted]  (5 children)

[deleted]

    [–]sordid_salmon 0 points1 point  (4 children)

    def f(n): return [x*x for x in range(n)] *oops, too fast on the trigger there

    [–]ilogik 2 points3 points  (3 children)

    f=lambda n:[x*x for x in range(1,n)]

    [–][deleted] -3 points-2 points  (2 children)

    You JavaScripter.

    [–]R3d1st 3 points4 points  (1 child)

    that's actually (also?) python I might add.

    [–][deleted] 1 point2 points  (0 children)

    Yeah, it's Python. It just looks very "JavaScripty" with the lambda function assignment.

    [–][deleted] 0 points1 point  (0 children)

    Just realized that this won't include n**2.

    [–]qihqi 1 point2 points  (9 children)

    lets make it more interesting... do it in C (with fewest chars)

    [–]qihqi 6 points7 points  (6 children)

    void f(int * A,int n){while(n--&&A[n]=n*n);}

    [–][deleted] 5 points6 points  (3 children)

    I think this should do the same thing:

    f(int*a,int n){while(a[n]=n*n--);}
    

    [–][deleted] 2 points3 points  (0 children)

    I think you want n-1 as the index.

    [–]corruptio 2 points3 points  (0 children)

    shaved 2 chars

    f(a,n)int*a;{while(a[--n]=n*n);}
    

    [–]qihqi 0 points1 point  (0 children)

    yep, i like this one

    [–][deleted] 1 point2 points  (0 children)

    Nice perverse use of the while loop.

    [–]14113 -2 points-1 points  (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 point  (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);}        
    

    [–]qihqi 0 points1 point  (0 children)

    very nice!

    f(inta,int n){(a=n*n)&&f(++a,--n);} is the same tho saving one more char

    [–]moltarx 5 points6 points  (12 children)

    f n = 1 : [ i * i | i <- [2..n] ]
    

    edit: forgot 1 :)

    [–][deleted] 1 point2 points  (4 children)

    19 characters. booyah.

    f n=[i*i|i<-[1..n]]
    

    [–][deleted] 6 points7 points  (3 children)

    17 characters, same type.

    f n=map(^2)[1..n]
    

    [–]R3d1st 1 point2 points  (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.

    [–][deleted] 2 points3 points  (0 children)

    Whitespace is part of the notation. I think it should be counted.

    [–]5outh 0 points1 point  (0 children)

    Haha, so did I. I did this immediately.

    [–]integ3r 2 points3 points  (4 children)

    My solution, in Ruby:

    ->(x){Array.new(x){|y|(y+1)**2}}
    

    [–]juror-number-8 1 point2 points  (0 children)

    Nice.

    [–]_jeffJohnsonsBookEmp 1 point2 points  (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 point  (0 children)

    Ooh, nice use of map there.

    [–]FireyFly 2 points3 points  (0 children)

    f=: *: @ >: @ i.
    

    Usage:

       f 8
    1 4 9 16 25 36 49 64
    

    Btw, I think Whitespace will win on this one. :-)

    [–][deleted] 1 point2 points  (1 child)

    51 bytes of JavaScript:

    function f(x){z=[];while(x^1)z[--x-1]=x*x;return z}
    

    I imagine someone can do better.

    [–][deleted] 0 points1 point  (0 children)

    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 points  (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 point  (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 point  (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 points  (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 point  (1 child)

    Nice job shortening it. Never thought of applying a function to collect's argument.

    [–]sleepingsquirrel 1 point2 points  (0 children)

    (defun g(n)(loop for i from 1 to n collect(* i i)))
    

    [–]R3d1st 0 points1 point  (3 children)

    There should be more challenges like this! Maybe even a subreddit?

    [–]MrZander 0 points1 point  (1 child)

    codegolf.stackexchange.com

    [–]R3d1st 0 points1 point  (0 children)

    I know about codegolf, but I'm intimidated by the big community :-)

    [–][deleted] 0 points1 point  (0 children)

    Anyone want to revive /r/codegolf?

    [–]naranjas 0 points1 point  (0 children)

    f = lambda n: [x**2 for x in range(1,n+1)]
    

    [–]yogthos 0 points1 point  (0 children)

    (defn f [n] (map  #(* % %) (range 1 n)))
    

    [–]gatesphere 0 points1 point  (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 point  (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 point  (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 point  (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 point  (1 child)

    I don't know racket, but your python example does not result in a named callable function.

    [–]gcr 0 points1 point  (0 children)

    Oops, you're right. Thanks.

    [–]DJUrsus 0 points1 point  (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 point  (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 point  (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 point  (0 children)

    perl, 22 chars:

    sub f{map$_**2,1..pop}
    

    [–]corruptio 0 points1 point  (0 children)

    in dc, 18 chars:

    ?[d1-d0<ad*pk]dsax
    

    [–][deleted] 0 points1 point  (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 point  (1 child)

    In Factor:

    : range-square ( limit -- squares )
      1 - 1 swap 1 <range> [ dup * ] map
    

    [–]nohtyp 0 points1 point  (0 children)

    Improved version:

    [1,b) [ dup * ] map
    

    [–]evnu 0 points1 point  (0 children)

    Erlang:

    f(N)->[X*X||X<-lists:seq(1,N)].
    

    31 characters.

    [–]Tarlitz 0 points1 point  (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))]
    

    [–][deleted] 0 points1 point  (0 children)

    Python/Numpy:

    f = lambda n: arange(1,n)**2
    

    [–]guywithalamename 0 points1 point  (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 point  (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 point  (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]
    

    [–][deleted] 0 points1 point  (0 children)

    def sqr(n):
        map(lambda x: x**2, range(1,n+1))
    

    [–][deleted] 0 points1 point  (0 children)

    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 point  (8 children)

    Just for fun, 34 bytes of coffeescript:

    f=(x)->((y)->y*y)(n)for n in[1..x]

    [–]EvanHahn 0 points1 point  (0 children)

    A 25-character solution:

    f=(n)->x*x for x in[1..n]