Lucky and even lucky numbers: Difference between revisions
Content deleted Content added
Add ref. |
J |
||
Line 91:
* Wiki entry [http://en.wikipedia.org/wiki/Lucky_number lucky_number].
<br>
=={{header|J}}==
Not going for extra credit because I want to encourage functional reactive "types" in J. (Note that FRP, along with an ML typed compiler, would probably remove the motivation for the while loop in this implementation.)
Implementation:
<lang J>luckySeq=:3 :0
1 luckySeq y
:
len=.0
nth=.0
seq=.x+2*i.4*y
while. len~:#seq do.
len=. #seq
nth=. nth+1
seq=. nth exclude seq
end.
)
exclude=: ] #~ 1 - #@] $ -@{ {. 1:
lucky=:''
evenLucky=:0
program=:3 :0
range=: |y-.0
seq=. (1+0 e.y) luckySeq >./range
if. 0><./y do.
(#~ e.&(thru/range)) seq
else.
(<:thru/range) { seq
end.
)
thru=: <./ + i.@(+*)@-~</lang>
Task:
<lang J> program 1 20
1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
program 1 20,evenLucky
2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
program 6000,-6100
6009 6019 6031 6049 6055 6061 6079 6093
program 6000,-6100,evenLucky
6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092</lang>
Note that I've used the J command line rather than a unix or windows command line. This is because J is portable to a wide variety of environments (including phones) and there's no reliably common command line that exists across all these environments. Therefore, J must provide its own, and J's command line requires some slight syntax changes from the suggestions implicit in this task.
=={{header|Perl 6}}==
|