Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Euler's Phi Function.

DZone's Guide to

Euler's Phi Function.

·
Free Resource
Returns the value of Euler's phi function for n. It basically calculates the number of relative prime integers to n smaller than n.

This only works if there the prime numbers smaller than n are stored in the array prime[] of nrPrime elements.

You can use this function to generate them:
http://snippets.dzone.com/posts/show/4189


int phi(int n) {
  double rez = n;

  int i = 0;
  while ((i < nrPrime) && (prime[i] <= n)) {
    if (n % prime[i] == 0) {
      rez *= (double)(prime[i] - 1) / (double)prime[i];
    }
    ++i;
  }

  return rez;
}


Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}