Last active
March 16, 2016 13:50
-
-
Save wang-zhijun/437c49677574d57bf24c to your computer and use it in GitHub Desktop.
Erlangで1000以内の素数を生成する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
main(_) -> | |
Res = prime_list(1000). | |
%% io:format("~p~n", [length(Res)]). | |
prime_list(Num)-> | |
prime_list(Num, 3, [2]). | |
prime_list(Num, PP, PS) when PP >= Num -> | |
lists:reverse(PS); | |
prime_list(Num, PP, PS) when PP < Num -> | |
IsPrime = lists:any(fun(X) -> PP rem X =:= 0 end, PS), | |
case IsPrime of | |
true -> prime_list(Num, PP+2, PS); | |
false -> prime_list(Num, PP+2, [PP|PS]) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment