Suddenly, I wrote a program to find a prime number in one line. Emphasis on code amount rather than efficiency.
Ruby
If you think it's filter
, it's select
in the case of Range. (Ruby 2.5)
(2..99).select {|x| (2...x).select {|i| x % i == 0} == []}
Python
You can use filter
, but list comprehension is easier to write.
[x for x in range(2, 100) if [i for i in range(2, x) if x % i == 0] == []]
Haskell
Haskell is stuck with beginners studying for years.
[x | x <- [2..100], [i | i <- [2..(x - 1)], mod x i == 0] == []]
When I thought that Ruby, which cannot include list comprehension, was disadvantageous, it was surprisingly the shortest I wrote.
Recommended Posts