Skip to content

Instantly share code, notes, and snippets.

@jvantuyl
Created August 3, 2009 02:32
Show Gist options
  • Save jvantuyl/160333 to your computer and use it in GitHub Desktop.
Save jvantuyl/160333 to your computer and use it in GitHub Desktop.
-module(perms).
-export([perms/1]).
% Calculate the Permutations of a list
perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
-module(sieve).
-export([primes/1]).
sieve([]) -> [];
sieve([H|T]) -> [H|sieve([X||X<-T,X rem H =/= 0])].
count(X) ->
count(X,[]).
count(X,L) when X >= 2 ->
count(X - 1,[X | L]);
count(X,L) when X < 2 ->
L.
% Find primes less than X using the Sieve of Eratosthenes
primes(X) ->
sieve(count(X)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment