[Question]
  [
Given an integer `n` (where `n < 10001`) as input, write a program that will output the first `n` [Ulam numbers](http://oeis.org/A002858). An Ulam number is defined as follows:
1. U1 = `1`, U2 = `2`.
2. For `n > 2`, Un is the smallest integer which is greater than Un-1 that is the sum of two **distinct** earlier terms in exactly one way.
For example, U3 is `3` (2+1), U4 is `4` (3+1) (note that (2+2) does not count as the terms are not distinct), and U5 is `6`, (U5 is not 5 because 5 can be represented as either 2+3 or 4+1). Here are the first few Ulam numbers:
> 
> `1, 2, 3, 4, 6, 8, 11, 13, 16, 18, 26, 28, 36, 38, 47, 48, 53, 57, 62, 69, 72, 77, 82, 87, 97, 99`
> 
> 
> 
This is code golf, so the shortest entry wins.
  
[Answer]
# CJam, ~~47~~ ~~41~~ 37 bytes
```
li4,1${__m*{_~<\:+*}%$2/z:^$2=+}*1><`
```
[Try it online.](http://cjam.aditsu.net/)
### Example run
```
$ cjam <(echo 'li4,1${__m*{_~<\:+*}%$2/z:^$2=+}*1><`') <<< 26
[1 2 3 4 6 8 11 13 16 18 26 28 36 38 47 48 53 57 62 69 72 77 82 87 97 99]
```
### How it works
This basic idea is the following:
1. Start with the array `A := [ 0 U₁ U₂ ... Uₖ ]`.
2. Compute `S`, the array of all sums `x + y` such that `x,y ∊ A` and `x < y`.
3. Discard all non-unique sums from `S`. Since every Ulam number greater than 2 is both the sum of two smaller ones and the sum of zero and itself, this discards the Ulam numbers `U₃, U₄, ... Uₖ`.
4. The remaining array is `[ U₁ U₂ Uₖ₊₁ ... ]`, so the next Ulam number is the third smallest element. Append it to `A` and go back to step 1.
```
li                                    " Read one integer (I) from STDIN.                  ";
  4,                                  " Push the array A = [ 0 1 2 3 ].                   ";
    1${                        }*     " Do the following I times:                         ";
       __m*                           " Push the Cartesian product A × A.                 ";
           {       }%                 " For each pair (x,y) in A × A:                     ";
            _~<\:+*                   " Compute (x + y) * (x < y).                        ";
                     $2               " Sort the resulting array.                         ";
                       /              " Split it into chunks of length 2.                 ";
                        z             " Transpose the resulting two-dimensional array.    ";
                         :^           " Compute the symmetric difference of its rows.     ";
                           $          " Sort the resulting array.                         ";
                            2=        " Extract its third element.                        ";
                              +       " Push it on the array A.                           ";
                                 1>   " Discard the first element of A (0).               ";
                                   <  " Discard all but the first I elements of A.        ";
                                    ` " Push a string representation of A.                ";
```
[Answer]
# J - 46 char
Function taking `n` as argument.
```
_2}.(,]<./@-.~</~({.+_*1<#)/.~@#&,+/~)@[&0&1 2
```
Explained by explosion:
```
    (                                )          NB. procedure for a list:
                                  +/~           NB.   take an addition table
              </~              #&,              NB.   select the top right half (no diag)
                 (        )/.~@                 NB.   for each unique value:
                       1<#                      NB.     if more than one present
                  {.+_*                         NB.     add infinity to it
      ]    -.~                                  NB.   remove existing Ulam numbers
       <./@                                     NB.   take the smallest
     ,                                          NB.   append to Ulam numbers
                                      @[&0      NB. repeat this procedure:
                                          &1 2  NB.   n times starting with [1, 2]
_2}.                                            NB. drop the last two numbers
```
[Answer]
## T-SQL, ~~301~~ ~~300~~ ~~288~~ 287
I've committed a little light SQL abuse.
```
DECLARE @N INT=100,@T INT=1DECLARE @ TABLE(I INT,U INT)INSERT @ VALUES(1,1),(2,2)#:IF @T>2INSERT @ SELECT TOP 1@T,A.U+B.U FROM @ A,@ B WHERE A.U>B.U GROUP BY A.U+B.U HAVING COUNT(*)=1AND A.U+B.U>ALL(SELECT U FROM @)ORDER BY 2SET @T+=1IF @T<=@N GOTO # SELECT U FROM @ WHERE I<=@N ORDER BY I
```
Try it in SQL Server 2008 [here](http://sqlfiddle.com/#!3/d41d8/39009).
@N holds the input integer. Change the example "100" to what n should be. "10000" will probably finish eventually, but I haven't let that run to completion. This entry's char count is for a one-digit input. Output is in query result form.
[Answer]
# Haskell, ~~70~~ 67 characters
```
u n=take n$1:2:[x|x<-[1..],[_]<-[[y|y<-u$n-1,z<-u$n-1,y<z,y+z==x]]]
```
usage:
```
>u 6
[1,2,3,4,6,8]
```
[Answer]
## GolfScript (41 37 bytes)
```
~.14*,3,\{1$.{2$1$-.@<*}%&,2=*|}/0-<`
```
[Online demo](http://golfscript.apphb.com/?c=Oyc2JwoKfi4xNCosMyxcezEkLnsyJDEkLS5APCp9JSYsMj0qfH0vMC08YA%3D%3D)
Cartesian products in GolfScript are quite long, so this takes a different approach. The long-term growth of the Ulam numbers is that the `n`th Ulam number is about `13.5n`, but in the first 10000 terms the greatest ratio between the `n`th Ulam number and `n` is just under `13.3`. So given `n` we can filter the first `14n` numbers to find those which belong in the sequence.
With thanks to [Dennis](https://codegolf.stackexchange.com/users/12012/dennis) for 41->37.
[Answer]
## JavaScript ES6, ~~100 ... 93~~ 90 characters
Run this in Web Console or Scratchpad of latest Firefox (Nightly or release).
**EDIT 8** Golfed a lot!!! and made it down to ~~94 characters~~ ~~93~~ 90 characters (thanks to @openorclose). (My first sub 100)
Here is my version which is much faster ~~but is 3 characters longer (107 characters)~~ ~~and is exactly the same amount of characters as above~~ and is much smaller than the brute force method below!, (thanks to edc65 ) :
```
u=n=>(s=>{for(r=[i=l=1];c=l<n;i+=c&&i-2?1:s[r[l++]=i]=1)r.map(j=>c-=j<i/2&s[i-j])})([])||r
```
I will keep trying to golf it further. But we are squeezing it out of the scope of JS :P
Here are some numbers when I run this inside a script tag in a webpage:
```
n           time (s)
10            0.001
100           0.005
1000          2.021
10000       236.983
100000      ~~pending~~ tldr; Too long didn't run :P
```
---
This is my first submission which is heavily inspired by @rink.attendant.6's answer in JavaScript.
```
u=n=>{for(l=[1,g=2],i=3;g<n;++i){z=1;for(j of l)for(k of l)z-=j<k&j+k==i;!z?l[g++]=i:0}return n>1?l:[1]}
```
I know this can be golfed even further. I will post a non-bruteforced solution too, which might be even shorter.
**EDIT 1**: Golfed a bit more and fixed for n = 1
I must say that I do envy Haskell and J for such super handy shortcuts for every kind of requirement -\_-
[Answer]
## Perl - 71 bytes
```
#!perl -p
@a=$b[2]=1;1while$b[++$a]^1||$_>map(++$b[$_+$a],@a)&&push@a,$a;$_="@a"
```
[Try it online!](https://tio.run/##K0gtyjH9r6xYAKQVdAv@OyTaqiRFG8XaGloblmdk5qQCedraKomxcYY1NSrxdrmJBRpAflK0SjxIVMchUVNNraC0OMMhUUcl0Vol3lbJIVHp/39DAwMDAA "Perl 5 – Try It Online")
Counting the shebang as one.  
Using a second array to store the sums seems to be significantly faster than a hash. Memory usage is also less, which I wouldn't have expected.
Sample usage:
```
$ echo 30 | perl ulam.pl
```
Sample output:
```
1 2 3 4 6 8 11 13 16 18 26 28 36 38 47 48 53 57 62 69 72 77 82 87 97 99 102 106 114 126
```
Approximate runtimes:
```
n = 100     0.015s
n = 1000    0.062s
n = 10000   4.828s
```
[Answer]
## Java, 259
```
import java.util.*;class C{public static void main(String[]a){List<Integer>l=new ArrayList<>();l.add(1);l.add(2);for(int i=3,z=0;l.size()<new Long(a[0]);i++,z=0){for(int j:l){for(int k:l){if(j<k&j+k==i)z++;}}if(z==1)l.add(i);}l.forEach(System.out::println);}}
```
Brute force works well for this.
```
import java.util.*;
class C {
    public static void main(String[] a) {
        List<Integer>l = new ArrayList<>();
        l.add(1);
        l.add(2);
        for (int i = 3, z = 0; l.size() < new Long(a[0]); i++, z = 0) {
            for (int j : l) {
                for (int k : l) {
                    if (j < k & j + k == i)
                        z++;
                }
            }
            if (z == 1)
                l.add(i);
        }
        l.forEach(System.out::println);
    }
}
```
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), ~~36~~ 35 bytes
*-1 byte by Adám*
```
{⍵↑{⍵,⊃∧(∊⊢⊆⍨⍧⍨∊2 3⍨)⍵~⍨,+⍀⍨⍵}⍣⍵⍳2}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/v/pR79ZHbRNBlM6jruZHHcs1HnV0Pepa9Kir7VHvike9y0FkR5eRgjGQoQlUVgekdbQf9TaApbfWPupdDDKjd7NR7f@0R20THvX2Peqb6ukPNO3QemOg2UBecJAzkAzx8Az@n6ZgyJWmYATEhgZcj3o69BXSFEwNAA "APL (Dyalog Extended) – Try It Online")
```
{⍵↑{⍵,⊃∧(∊⊢⊆⍨⍧⍨∊2 3⍨)⍵~⍨,+⍀⍨⍵}⍣⍵⍳2}      Monadic function taking an argument n:
{⍵,⊃∧(∊⊢⊆⍨⍧⍨∊2 3⍨)⍵~⍨,+⍀⍨⍵}   Helper function to compute the next Ulam number
                                    given ⍵ (the first few Ulam numbers)
                        +⍀⍨⍵      Make an addition table from ⍵.
                       ,          Flatten into a list.
                   ⍵~⍨            Remove all entries already in ⍵.
     (∊⊢⊆⍨2 3∊⍨⍧⍨)               Helper function taking an argument x:
                ⍧⍨                  The count of elts of x in itself                 
           2 3∊⍨                    1s where those counts are in (2 3), else 0s.*
       ⊢⊆⍨                          Partition x, removing values corresponding to 0s.
      ∊                             Join the partitions into a single list.
    (∊⊢⊆⍨⍧⍨∊2 3⍨)                Keep all elements that occur exactly 2 or 3 times.
                                  (i.e. that occur once as a
                                  sum of distinct elements of ⍵).
    ∧                         Sort ascending.
   ⊃                          Take the first value (the next Ulam #).
 ⍵,                           Append that value to ⍵.
{⍵↑{...}⍣⍵⍳2}
{  {...}⍣⍵  }                 Call the helper function n times
           ⍳2                 starting with (1 2). First n+2 Ulam numbers.
 ⍵↑                           Keep the first n elements.
```
When we make the addition table, diagonal elements are twice the entries in ⍵. Non-diagonal elements are sums of distinct elements, with each \$x\$ occurring twice for each way \$x\$ can be written as a sum of distinct elements. Therefore, each number \$x\$ occurs \$2a+b\$ times, where \$a\$ is the number of ways \$x\$ can be written as a sum of distinct elements from ⍵, and \$b\$ is 1 iff \$x\$ is twice some entry in ⍵. We want \$a=1\$, so it is sufficient to check \$2a+b \in \{ 2, 3\}\$.
\* (In ngn/APL, a constant can end a train without using `⍨`. But ngn/APL doesn't have count-in, so we need ⍨ somewhere.)
[Answer]
# PHP 5.4+, 164
Same approach as my answers:
```
<?function u($n){for($l=[1,2],$i=3;count($l)<$n;++$i){$z=0;foreach($l as $j){foreach($l as $k){$z+=$j<$k&$j+$k==$i;}}if($z==1)$l[]=$i;}return array_slice($l,0,$n);}
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 20 bytes
```
Œc§ḟµḟœ-Q$Ṃɓ;
2RÇ⁸¡ḣ
```
[Try it online!](https://tio.run/##ATEAzv9qZWxsef//xZJjwqfhuJ/CteG4n8WTLVEk4bmCyZM7CjJSw4figbjCoeG4o////zEw "Jelly – Try It Online")
```
Œc§ḟµḟœ-Q$Ṃɓ;    Helper link that appends the next number to x, a list of Ulam numbers:
Œc                  All unordered pairs of x
  §                 Sum each pair
   ḟ                Filter out the numbers already present in x.
    µ               Let this list be y. Then apply the following chain:
     œ-Q$Ṃ          Find the minimum of all unique elements.
     ḟ                Take y and filter out the elements in
      œ-Q$            the multiset difference between y and its unique elements.
          Ṃ           Then find the Ṃinimum of the result.
           ɓ;    Append (ɓ reverses argument order) the result to 
2RÇ⁸¡ḣ           Main link:
2R               Start with [1,2].
  Ç⁸¡            Apply the helper link (Ç) n (⁸) times to generate n+2 Ulam #s.
     ḣ           Keep the first n values.
```
[Answer]
## CoffeeScript, ~~119~~ 114
Lately I've been practising CoffeeScript to improve at golfing JavaScript, so here's my JavaScript answer compiled into CoffeeScript:
```
u=(n)->l=[1,2];i=3;z=0;(for j in l
 for k in l
  z+=j<k&j+k==i
l.push(i) if z==1;++i;z=0)while l.length<n;l[..n-1]
```
I don't understand [loops and comprehensions in CoffeeScript](http://coffeescript.org/#loops) very well so perhaps this can be golfed further but it's what I have for now. Newlines are counted as one character (Unix style).
[Answer]
## JavaScript, ~~147~~ ~~154~~ 150 (136)
Heavily inspired by @Ypnypn's brute-force Java solution posted earlier:
```
function u(n){for(l=[1,2],i=3;l.length<n;++i){z=0;l.forEach(function(j){l.forEach(function(k){z+=j<k&j+k==i})});if(z==1)l.push(i)}return l.slice(0,n)}
```
Thanks for @Dennis for shaving 4 to 18 bytes off my original version
### Dangerous version (using [`for..in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loops)
I would not recommend running this because looping through an object that is an [`instanceof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof) [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) using a `for..in` loop could cause your machine to burst into flames and/or transform into an angry killing machine, but here it is:
```
function u(n){for(l=[1,2],i=3;l.length<n;++i){z=0;for(j in l)for(k in l)z+=l[j]<l[k]&l[j]+l[k]==i;if(z==1)l.push(i)}return l.slice(0,n)}
```
### Ungolfed
```
function u(n) {
    var l = [1, 2],
        i = 3,
        j, k, z;
    for (; l.length < n; ++i) {
        z = 0; 
        l.forEach(function (j) {
            l.forEach(function (k) {
                if (j < k & j + k === i) {
                    z++;
                }
            });
        });
        if (z === 1) {
            l.push(i);
        }
    }
    return l.slice(0, n);
}
```
[Answer]
# Mathematica, ~~107~~ 91 bytes
```
Nest[#~Append~Min@Cases[Tally[Tr/@#~Subsets~2],{n_,1}:>n]&,{1,2},i=Input[]]~Drop~{3}~Take~i
```
It's a very direct implementation of the spec.
* Find all pairs.
* Delete all duplicates.
* Delete all numbers less than the last Ulam number.
* Append the minimum to the list.
I'm also applying Dennis's trick of including sums with `0`, but the catch is that this makes the third element of the list `0` before resuming as one would expect, so I need to remove that element from the list.
It handles an input of `1000` in a few seconds, but I doubt that you'll get a result for 10k in a reasonable amount of time. But I don't think any of the others performs well on that either.
[Answer]
# OCaml - 254 Characters
The code use an hash table to store the sum of the current elements of the list and update it each time a new element is computed.
```
open Hashtbl let h=create 7 let()=add h 3 1 let rec r n i l=if n=0then List.rev l else if mem h i&&find h i=1then(List.iter(fun x->if mem h(x+i)then replace h(x+i)2else add h(x+i)1)l;r(n-1)(i+1)(i::l))else r n(i+1)l let u n=if n=1then[1]else r(n-2)3[2;1]
```
## Usage:
Within OCaml interpreter:
```
# u 26;;
- : int list =
[1; 2; 3; 4; 6; 8; 11; 13; 16; 18; 26; 28; 36; 38; 47; 48; 53; 57; 62; 69;
 72; 77; 82; 87; 97; 99]
```
## Ungolfed
```
open Hashtbl
let h = create 7
let() = add h 3 1
let rec r n i l =
  if n=0 then List.rev l
  else if mem h i && find h i=1 then
    begin
      List.iter
        (fun x-> if mem h(x+i) then replace h (x+i) 2 else add h (x+i) 1)
        l;
      r (n-1) (i+1) (i::l)
    end
  else r n (i+1) l
let u n = if n=1 then [1] else r (n-2) 3 [2;1]
```
[Answer]
# Python, ~~137~~ ~~128~~ 126 characters.
```
U,i=[1,2],2
for _ in [[0]]*(input()-2):
 t=_*3*i
 for a in U:
  for b in U:t[a+b]+=a!=b
 i=t[i+1:].index(2)+i+1;U+=[i]
print U
```
This is my first golf, and I've brought it down from ~250 characters, I'm pretty happy but would love suggestions on how to improve!
[Answer]
# C#, 257
Brute force approach, using LINQ:
```
using System.Linq;class U{void F(int n){var u=n<2?new int[]{1}:new int[]{1,2};for(int i=3;u.Length<n;++i)if(u.SelectMany(x=>u,(a,b)=>new{A=a,B=b}).Count(x=>x.A>x.B&&x.A==i-x.B)==1)u=u.Union(new int[]{i}).ToArray();System.Console.Write(string.Join("",u));}}
```
## Ungolfed, With Test Harness
```
using System.Linq;
class Ulam
{
    void F(int n)
    {
        //handle special case where n = 1 (ugh)
        var u = n < 2 ? new int[] { 1 } : new int[] { 1, 2 };
        for (int i=3; u.Length<n; ++i)
            if (u.SelectMany(x => u, (a, b) => new { A = a, B = b })
                     .Count(x => x.A > x.B && x.A == i - x.B) == 1)
                u = u.Union(new int[] { i }).ToArray();
        System.Console.Write(string.Join(" ",u));
    }
    public static void Main(string[] args)
    {
        new Ulam().F(1);
        System.Console.WriteLine();
        new Ulam().F(2);
        System.Console.WriteLine();
        new Ulam().F(3);
        System.Console.WriteLine();
        new Ulam().F(26);
        System.Console.WriteLine();
    }
}
```
[Answer]
# Pyth, ~~27~~ 25 bytes
```
<uaGh-sfq1lT.gksM.cG2GQS2
```
Try it online [here](https://pyth.herokuapp.com/?code=%3CuaGh-sfq1lT.gksM.cG2GQS2&input=10&debug=0).
```
<uaGh-sfq1lT.gksM.cG2GQS2Q   Implicit: Q=eval(input())
                             Trailing Q inferred
 u                    Q      Perform the following Q times...
                       S2    ... with G initialised to [1,2]:
                 .cG2          Get all 2-element combinations of G
               sM              Sum each pair
            .gk                Group them by value
                                 The groups are sorted by the result of the sum
       f                       Filter the groups, as T, keeping those where:
          lT                     Length of T
        q1                       Equal to 1
      s                        Flatten list
     -               G         Remove elements of the above which are already in G
    h                          Take the first of the remaining elements
                                 This is the smallest, as the grouping also sorted them
  aG                           Append this to G
<                        Q   Take the first Q elements, implicit print
```
*Edit: golfed 2 bytes by performing summation before grouping. Previous version: `<uaGh-mssdfq1lT.gsk.cG2GQS2`*
[Answer]
# C, 478 bytes
```
#define R return
bs(x,v,l,h,r)unsigned x,*v,l,h,*r;{unsigned m;for(;l<=h;){m=(l+h)/2;if(x<v[m])h=m-1;else if(x>v[m])l=m+1;else{*r=m;R 1;}}*r=m;R 0;}
#include<stdlib.h>
unsigned*f(unsigned w){unsigned*u=0,i,k,m,y,z;if(w>1E6||w==0)R u;u=malloc(w*sizeof*u);if(!u)R u;k=0;u[k++]=1;if(w==1)R u;m=u[k++]=2;if(w==2)R u;l:for(i=0,y=0,z=k-1,++m;i<k;y+=bs(m-u[i],u,i+1,z,&z),++i)if(y>1||u[i]+(i+1!=k?u[i+1]:0)>m)break;if(m==0){free(u);u=0;R u;}if(y!=1)goto l;u[k++]=m;if(k< w)goto l;R u;}
```
In Tio now in 9 seconds it would find 10000 values (and in there print the first 100 values). The trick is using not linear search in the inner loop but binary search...
These below are functions well indented and full readable (at last for me):
```
bsCopy(x,v,l,h,r)unsigned x,*v,l,h,*r;
{unsigned m;
 for(;l<=h;){m=(l+h)/2;if(x<v[m])h=m-1;else if(x>v[m])l=m+1;else{*r=m;R 1;}}
 *r=m;R 0;// in *r if return 0 the min index that fail else the index of find x
}
unsigned*fCopy(unsigned w)
{unsigned*u=0,i,k,m,y,z;
 if(w>1E6||w==0)R u;
 u=malloc(w*sizeof*u);
 if(!u)R u;
 k=0;u[k++]=1;if(w==1)R u;
   m=u[k++]=2;if(w==2)R u;//below I suppose m-u[i] is in the range (if exist in u) (i+1)..z 
 l: for(i=0,y=0,z=k-1,++m;i<k;y+=bsCopy(m-u[i],u,i+1,z,&z),++i)
          if(y>1||u[i]+(i+1!=k?u[i+1]:0)>m)break;
   if(m==0){free(u);u=0;R u;}
          if(y!=1)goto l;
   u[k++]=m;if(k< w)goto l;
 R u;
}
```
[Answer]
# APL(NARS), 278 char, 556 bytes
```
∇u←p w;m;y;i;k;z;r;bs
bs←{(x l h)←⍵⋄l>h:0,h⋄x<⍺[t←⌊2÷⍨l+h]:⍺∇x,l,t-1⋄x>⍺[t]:⍺∇x,(t+1),h⋄1,t}
u←⍬  ⋄→0×⍳(w>1E6)∨w≤0
u←u,1⋄→0×⍳w=1
u←u,2⋄→0×⍳w=2⋄k←m←2
i←1⋄y←0⋄m+←1⋄z←k
→7×⍳(y>1)∨i>k⋄→7×⍳m<u[i]+{i=k:0⋄u[i+1]}⋄r←u bs(m-u[i]),(i+1),z⋄y+←↑r⋄z←2⊃r⋄i+←1⋄→6
→5×⍳y≠1⋄u←u,m⋄k+←1⋄→5×⍳k<w
∇
```
it would be the translation in APL of C one I sent.
It seems i not understand when to use ∇∇ in place of ∇... possible ∇∇ is used when there is one
argument is one function (and not one other type).
"u bs x, a,b" should be the bin search in "u" array for the value "x" in the range a..b; it would return 1, indexWhereFind
or 0, indexWhereEndOfsearch. With argument 200 p function take +- a minute here...
```
  p 100
1 2 3 4 6 8 11 13 16 18 26 28 36 38 47 48 53 57 62 69 72 77 82 87 97 99 102 106 114 126 
  131 138 145 148 155 175 177 180 182 189 197 206 209 219 221 236 238 241 243 253 
  258 260 273 282 309 316 319 324 339 341 356 358 363 370 382 390 400 402 409 412 
  414 429 431 434 441 451 456 483 485 497 502 522 524 544 546 566 568 585 602 605 
  607 612 624 627 646 668 673 685 688 690 
  p¨1 2 3 4
1  1 2  1 2 3  1 2 3 4 
```
]