Skip to content

Instantly share code, notes, and snippets.

@badboy
Created March 20, 2009 13:04
Show Gist options
  • Save badboy/82353 to your computer and use it in GitHub Desktop.
Save badboy/82353 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void prim(int n)
{
if(n<2) return;
printf("2, ");
if(n==2) return;
int p = 3, t;
int found;
for(p=3;p<=n;p+=2)
{
found = 0;
for(t=3;pow(t,2)<=p;t+=2)
{
if(p%t == 0)
{
found = 1;
break;
}
}
if(!found) printf("%d, ", p);
}
}
int main(int argc, char** argv)
{
prim(200);
return 0;
}
def getPrim(n)
return nil if n < 2
(p 2; return 2) if n == 2
prims = [2]
p = 3
while p <= n
t = 3
found = false
while t**2 <= p
if(p%t == 0)
found = true
break
end
t+=2
end
prims << p if(!found)
p+=2
end
prims
end
p getPrim((200))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment