Skip to content

Instantly share code, notes, and snippets.

@alvesjnr
Created September 22, 2010 02:21
Show Gist options
  • Save alvesjnr/591005 to your computer and use it in GitHub Desktop.
Save alvesjnr/591005 to your computer and use it in GitHub Desktop.
/**
License: Creative Comons
Author: Antonio Ribeiro Alves júnior
**/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int *primo(int v)
{
int *k = (int*)malloc(sizeof(int)*v);
int *result;
int cont = 2;
int i, raiz, j;
for(i=0; i<v; i++)
k[i] = i;
i=2;
raiz = (int)sqrt((float)v);
while(i<=raiz)
{
j = i;
if(k[j] != 0)
{
while(j <= v-1)
{
j+=i;
if(j <= v)
k[j]=0;
}
}
i++;
}
cont = 0;
for(i=0; i<=v; i++)
if(k[i]!=0)
cont++;
result = (int*)malloc(sizeof(int)*(cont+1));
result[0] = cont;
cont = 1;
for(i=0; i<=v; i++)
if(k[i]!=0)
result[cont++]=k[i];
free(k);
return result;
}
int main()
{
int *a, i;
a = (int*)primo(10000000);
for(i=0; i<= a[0]; i++)
printf("%i ",a[i]);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment