Skip to content

Instantly share code, notes, and snippets.

@mattflo
Created January 18, 2012 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattflo/1630320 to your computer and use it in GitHub Desktop.
Save mattflo/1630320 to your computer and use it in GitHub Desktop.
primes = function(number){
divisor = 2;
while (divisor<number)
{
if (number % divisor == 0) return [divisor].concat(primes(number/divisor));
divisor = divisor + 1;
}
if (number == 1) return [];
return [number];
};
describe("primes", function(){
it("1 is empty", function(){
expect(primes(1)).toEqual([]);
});
it("2 is 2", function(){
expect(primes(2)).toEqual([2]);
});
it("3 is 3", function(){
expect(primes(3)).toEqual([3]);
});
it("4 is 2,2", function(){
expect(primes(4)).toEqual([2,2]);
});
it("5 is 5", function(){
expect(primes(5)).toEqual([5]);
});
it("6 is 2,3", function(){
expect(primes(6)).toEqual([2,3]);
});
it("7 is 7", function(){
expect(primes(7)).toEqual([7]);
});
it("8 is 2,2,2", function(){
expect(primes(8)).toEqual([2,2,2]);
});
it("9 is 3,3", function(){
expect(primes(9)).toEqual([3,3]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment