Skip to content

Instantly share code, notes, and snippets.

@shawon100
Forked from tahmidrafi/sieve_phi.cpp
Created February 23, 2017 16:49
Show Gist options
  • Select an option

  • Save shawon100/9fa69300d7257e954ba2772afab1a122 to your computer and use it in GitHub Desktop.

Select an option

Save shawon100/9fa69300d7257e954ba2772afab1a122 to your computer and use it in GitHub Desktop.
#include<stdio.h>
int phi[1000006], mark[1000006];
void seivephi(int n)
{
int i, j;
for(i=1; i<=n; i++) phi[i] = i;
phi[1] = 1;
mark[1] = 1;
for(i=2; i<=n; i++)
if(!mark[i])
for(j=i; j<=n; j+=i)
{
mark[j] = 1;
phi[j] = phi[j] / i * (i-1);
}
}
int main()
{
int n = 30, i;
seivephi(n);
printf(" i: ");
for(i=1; i<=n; i++)
printf("%3d%c", i, i<n?' ':'\n');
printf(" phi: ");
for(i=1; i<=n; i++)
printf("%3d%c", phi[i], i<n?' ':'\n');
printf("mark: ");
for(i=1; i<=n; i++)
printf("%3d%c", mark[i], i<n?' ':'\n');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment