Skip to main content
prefer "in" over contains
Source Link

Groovy : 122 118118 111 chars

Golfed:

m=args[0] as int
a=[0]
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&(!a.contains(x) in a))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = [0]
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    ( x>0 & (!a.contains(x) in a) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23

Groovy : 122 118 chars

Golfed:

m=args[0] as int
a=[0]
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&(!a.contains(x)))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = [0]
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    ( x>0 & (!a.contains(x)) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23

Groovy : 122 118 111 chars

Golfed:

m=args[0] as int
a=[0]
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&!(x in a))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = [0]
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    ( x>0 & !(x in a) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23
incremental golf improvement
Source Link

Groovy : 122122 118 chars

Golfed:

m=args[0] as int
a=[]
a<<0a=[0]
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&&x>0&(!a.contains(x)))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = []
a << 0[0]
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    ( x>0 &&& (!a.contains(x)) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23

Groovy : 122 chars

Golfed:

m=args[0] as int
a=[]
a<<0
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&&(!a.contains(x)))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = []
a << 0
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    (x>0 && (!a.contains(x)) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23

Groovy : 122 118 chars

Golfed:

m=args[0] as int
a=[0]
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&(!a.contains(x)))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = [0]
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    ( x>0 & (!a.contains(x)) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23
Source Link

Groovy : 122 chars

Golfed:

m=args[0] as int
a=[]
a<<0
(1..m-1).each{n->b=a[n-1];x=b-n;(x>0&&(!a.contains(x)))?a[n]=x:(a[n]=b+n)}
a.each{print "$it "}

Ungolfed:

m = args[0] as int
a = []
a << 0
(1..m-1).each { n->
    b = a[n-1]
    x = b-n
    (x>0 && (!a.contains(x)) ) ? a[n] = x : (a[n] = b+n) 
}
a.each{print "$it "}

Sample Run:

bash$ groovy Rec.groovy 14
0 1 3 6 2 7 13 20 12 21 11 22 10 23