Created
November 16, 2014 02:00
-
-
Save tshrkmd/976e62fe04dae22b442f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include | |
#include | |
/** | |
* 構造体 | |
*/ | |
typedef struct sosu_list { | |
int no; | |
struct sosu_list *next; | |
} SOSU_LIST; | |
/** | |
* 構造体の素数番号を返却 | |
*/ | |
int get_sosu_list( SOSU_LIST *head , int no ) | |
{ | |
SOSU_LIST *p = head->next; | |
int count = 0; | |
while( p != NULL && count < no ) | |
{ | |
p = p->next; | |
count ++; | |
} | |
if( count >= no ) return p->no; | |
return -1; | |
} | |
/** | |
* 素数計算 | |
*/ | |
void calc_sosu( SOSU_LIST *head , int max ) | |
{ | |
int add_flg = 1; | |
int count = 2; | |
int sosu_count = 0; | |
SOSU_LIST *list_p = head->next; | |
SOSU_LIST *end_p = head; | |
SOSU_LIST *sosu_p = NULL; | |
while( sosu_count < max ) | |
{ | |
list_p = head->next; | |
add_flg = 1; | |
while( list_p != NULL ) | |
{ | |
if( ( count % list_p->no ) == 0 ) | |
{ | |
add_flg = 0; | |
break; | |
} | |
list_p = list_p->next; | |
} | |
if( add_flg == 1 ) | |
{ | |
sosu_p = ( SOSU_LIST *) malloc( sizeof( SOSU_LIST )); | |
if (sosu_p == NULL) { | |
return ; | |
} | |
sosu_p->no = count; | |
sosu_p->next = NULL; | |
end_p->next = sosu_p; | |
end_p = sosu_p; | |
sosu_count ++; | |
//printf("%dn",end_p->no); | |
} | |
count++; | |
} | |
} | |
/** | |
* エントリポイント | |
*/ | |
int main() | |
{ | |
SOSU_LIST head; | |
head.no = 0; | |
head.next = NULL; | |
calc_sosu( &head ,1001 ); | |
printf("1000番目の素数は%dです。n", get_sosu_list( &head, 1000)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment