Skip to content

Instantly share code, notes, and snippets.

@ryuichimatsumoto-single
Created June 25, 2016 11:47
Show Gist options
  • Save ryuichimatsumoto-single/807d5804bf1ee3f8490b7b55a7b9ae9f to your computer and use it in GitHub Desktop.
Save ryuichimatsumoto-single/807d5804bf1ee3f8490b7b55a7b9ae9f to your computer and use it in GitHub Desktop.
#include<stdio.h>
long int is_prime(long int n)
{
int bool = 0;//nが素数かどうかの判定
if(n > 1)
{
long int count = 0;//2からnまで走査し、割り切れたものをカウント
for(long int k=1;k<(n+1);k++)
{
if(n%k == 0)
{
count++;
}
}
bool = (count == 2) ? 1 : 0;
}
else
{
bool = 1;//1の場合は割り切れる整数の個数が1個となるので、例外処理
}
return bool;
}
/*1からnまでの整数の個数について調べる*/
long int number_of_prime(long int n)
{
int count = 0;
for(long int k=1;k<(n+1);k++)
{
count += is_prime(k);
}
return count;
}
int main()
{
/*
printf("number_of_prime[1,10]=%ld\n",number_of_prime(10));
printf("number_of_prime[1,100]=%ld\n",number_of_prime(100));
printf("number_of_prime[1,1000]=%ld\n",number_of_prime(1000));
printf("number_of_prime[1,10000]=%ld\n",number_of_prime(10000));
*/
printf("number_of_prime[1,100000]=%ld\n",number_of_prime(20000));
//printf("number_of_prime[1,1000000]=%ld\n",number_of_prime(1000000));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment