Skip to content

Instantly share code, notes, and snippets.

@keizo042
Created August 8, 2014 16:14
Show Gist options
  • Save keizo042/a60f1b83f3d272a4f042 to your computer and use it in GitHub Desktop.
Save keizo042/a60f1b83f3d272a4f042 to your computer and use it in GitHub Desktop.
エラトステネスの篩を実装したかったが断念
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define LEN 1000
#define PRIME int
struct list{
struct list *next;
int num;
};
void p_init(PRIME *p);
int* prime_f(PRIME *p);
void clean(int n,PRIME *p);
void show(struct list *l);
int
main(int argc, char **argv)
{
PRIME list[100];
int *p = NULL;
p_init(list);
p = prime_f(list);
return 0;
}
void p_init(PRIME *p)
{
int i;
for(i=0; i < LEN;i++)
{
*(p+i) = 1;
}
}
/*
void push(int n, struct list *p)
{
if( p == NULL)
{
p = (struct list*)malloc( sizeof(struct list*) );
}else{
p->next = (struct list*)malloc( sizeof(struct list*) );
p = p->next;
}
p->num = n;
p->next = NULL;
}
*/
void push(int n, int *p)
{
*(p+n) = n;
}
void clean(int n,PRIME *p)
{
int i;
for(i =0; i <= (int)sqrt(LEN); i = i +n)
{
*(p+i)=0;
}
}
int* prime_f(PRIME *p)
{
int i,a=0;
int array[LEN];
for(i = 0; i < (int)sqrt((double)LEN); i++)
{
if( *(p+i) == 1 )
{
push(i,array);
clean(i,p);
}
}
return array;
}
void show(struct list *l)
{
struct list *p =l;
while(p != NULL)
{
printf("%d ",p->num);
p = p->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment