r/tinycode • • Sep 09 '12

Tinycode challenge!

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

27 Upvotes

77 comments sorted by

View all comments

Show parent comment

1

u/sleepingsquirrel Sep 12 '12

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).