Skip to content

Instantly share code, notes, and snippets.

@slntopp
Last active July 13, 2021 12:24
Show Gist options
  • Save slntopp/e2a7ac708b6485dbd8535b09ec66e6f6 to your computer and use it in GitHub Desktop.
Save slntopp/e2a7ac708b6485dbd8535b09ec66e6f6 to your computer and use it in GitHub Desktop.
Codewars Kata Attempt | Number of Proper Fractions with Denominator d | C++
unsigned long long properFractions(unsigned long long n) {
if(n == 1) return 0;
unsigned long long d = n;
for(unsigned long long i = 2; i * i <= d; i++) {
if(d % i == 0) {
n = n / i * (i - 1);
while(d % i == 0) d /= i;
}
}
if (d > 1) n = n / d * (d - 1);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment