Hacker News new | past | comments | ask | show | jobs | submit login

If the goal is conciseness, let's use K. Start with a range:

      1+!20
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Prepend runs of 0 of each length with 1:

      (1,&:)'1+!20
    (1 0
     1 0 0
     1 0 0 0
     1 0 0 0 0
     1 0 0 0 0 0
     1 0 0 0 0 0 0
     1 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
Cycle each pattern to form a matrix:

      (20#1,&:)'1+!20
    (1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
     1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0
     1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
     1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
     1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0
     1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
     1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
     1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
     1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
Sum the rows of the matrix:

      +/(20#1,&:)'1+!20
    20 0 1 1 2 1 3 1 3 2 3 1 5 1 3 3 4 1 5 1
Find indices with exactly one factor:

      &1=+/(20#1,&:)'1+!20
    2 3 5 7 11 13 17 19
Generalize to any upper limit x:

      {&1=+/(x#1,&:)'1+!x}100
    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
As an afterthought, I realized it's easier and equivalent to work from a 0-based range:

      {&2=+/(x#1,&:)'!x}100
    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
I couldn't precisely remember this function, but I'd seen the approach before and it was easy to build up piece by piece in the REPL.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: