SoFunction
Updated on 2025-03-10

Example of Euler function simple implementation of PHP

This article describes the simple implementation of Euler function Euler function in PHP. Share it for your reference, as follows:

Euler functionph(n) meansAll numbers smaller than n and co-equality with n

For example ph(10) = 4{1,3,7,9 and 10}

The code is as follows:

<?php
function Euler($x)
{
  $res = $x;
  $now = 2;
  while ($x > 1) {
    if ($x % $now == 0) {
      $res /= $now;
      $res *= ($now - 1);
      while ($x % $now == 0) {
        $x /= $now;
      }
    }
    $now++;
  }
  return $res;
}
$res = Euler(10);
var_dump($res);
?>

Running results:

int(4)

For more information about PHP related content, please check out the topic of this site:PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of usage of php strings》、《Complete collection of PHP array (Array) operation techniques》、《Summary of common traversal algorithms and techniques for PHP"and"Summary of PHP mathematical operation skills

I hope this article will be helpful to everyone's PHP programming.