The number of coprime numbers (short division factorization + Euler function)

Description of the meaning of the question: Given an n, find the number of 1-n co-prime numbers.

#include<cstdio>
#include<cmath>
using namespace std;
int res[100000];
int main(){
	int n;scanf("%d",&n);
	int ans=n;
	int tot = 0;
	for(int i=2;i*i<n;i++){
		if (n% i == 0) res [++ tot] = i;
		while(n%i==0)n/=i;
	}
	if(n>1)res[++tot]=n;
	for(int i=1;i<=tot;i++){
		ans=ans-ans/res[i];
	}
	printf("%d\n",ans);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326077255&siteId=291194637