Y
Y
Yaroslav Nikitin2014-04-30 21:26:19
C++ / C#
Yaroslav Nikitin, 2014-04-30 21:26:19

How to use the Euler function in C++?

I can't figure out how to use it at all. I copied the int function from the book.

#include <iostream>
#include <conio.h>
using namespace std;

  int phi (int n) {
    int result = n;
    for (int i=2; i*i<=n; ++i){
      if (n % i == 0) {
        while (n % i == 0)
          n /= i;
        result -= result / i;
      }
    }
    if (n > 1){
      result -= result / n;
    }
    return result;
  }

int main(){
  /*что-то*/
  _getch();
  return 0;
}

Tell me how to add the code to a working state.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-04-30
@copyloc

first is the phi function which returns an int. Secondly, read on Wikipedia what this function does in general, and the question will disappear.

Y
Yaroslav Nikitin, 2014-04-30
@copyloc

Everything is clear, I misunderstood this Function a little.

#include <iostream>
#include <conio.h>
using namespace std;

  void phi(){
    int n,result;
    cin >> n;
    result = n;
    for (int i = 2; i*i <= n; ++i){
      if (n % i == 0){
        while (n % i == 0){
          n /= i;
        }
          result -= result / i;
      }
    }
    if (n > 1){
      result -= result / n;
    }
    cout << result;
  }

int main(){
  phi();
  _getch();
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question