PHP code example of macocci7 / php-math-integer

1. Go to this page and download the library: Download macocci7/php-math-integer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

macocci7 / php-math-integer example snippets


  

  occi7\PhpMathInteger\Number;

  $n = new Number();

  echo "Is 1 int? - " . ($n->isInt(1) ? 'Yes' : 'No') . ".\n";
  echo "Are [ 1, 2, 3, ] all int? - " . ($n->isIntAll([ 1, 2, 3, ]) ? 'Yes' : 'No') . ".\n";
  echo "Is 1 natural? - " . ($n->isNatural(1) ? 'Yes' : 'No') . ".\n";
  echo "Are [ 1, 2, 3, ] all natural? - " . ($n->isNaturalAll([ 1, 2, 3, ]) ? 'Yes' : 'No') . ".\n";
  echo "Is 1.2 float? - " . ($n->isFloat(1.2) ? 'Yes' : 'No') . ".\n";
  echo "Are [ -2.1, 0.0, 1.2, ] all float? - " . ($n->isFloatAll([ -2.1, 0.0, 1.2, ]) ? 'Yes' : 'No') . ".\n";
  echo "Is 1.2 number? - " . ($n->isNumber(1.2) ? 'Yes' : 'No') . ".\n";
  echo "Are [ -2, 0.1, 3, ] all number? - " . ($n->isNumberAll([ -2, 0.1, 3, ]) ? 'Yes' : 'No') . ".\n";
  echo "Is 0.1 fraction? - " . ($n->isFraction(0.1) ? 'Yes' : 'No') . ".\n";
  echo "Are [ -0.99, 0.1, 0.99, ] all fraction? - " . ($n->isFractionAll([ -0.99, 0.1, 0.99, ]) ? 'Yes' : 'No') . ".\n";
  echo "Sign of -2.5 is " . $n->sign(-2.5) . ".\n";
  echo "Integer part of 3.14 is " . $n->int(3.14) . ".\n";
  echo "Fractional part of 3.14 is " . $n->fraction(3.14) . ".\n";
  echo "-3th digit of 123.4567 is " . $n->nthDigit(-3, 123.4567) . ".\n";
  echo "Number of digits -123.4567 is " . $n->numberOfDigits(-123.4567) . ".\n";
  echo "Number of fractional digits -12.3456 is " . $n->numberOfFractionalDigits(-12.3456) . ".\n";
  

  

  occi7\PhpMathInteger\Prime;

  $p = new Prime();

  // judge if $n is prime or not
  $n = 3;
  echo sprintf("Is %d prime? - %s.\n", $n, $p->isPrime($n) ? 'Yes' : 'No');

  // judge if all of $n are prime or not
  $n = [ 2, 3, 5, ];
  echo sprintf(
      "Are all of [%s] prime? - %s.\n\n",
      implode(', ', $n),
      $p->isPrimeAll($n) ? 'Yes' : 'No'
  );

  // a prime previous to $n
  $n = 5;
  echo sprintf("A prime previous to %d is %d.\n", $n, $p->previous($n));

  // a prime next to $n
  $n = 5;
  echo sprintf("A prime next to %d is %d.\n\n", $n, $p->next($n));

  // primes between $a and $b
  $a = 6;
  $b = 14;
  echo sprintf(
      "Primes between %d and %d are [%s].\n\n",
      $a,
      $b,
      implode(', ', $p->between($a, $b))
  );

  // factorize
  $n = 1234567890;
  echo sprintf("Factorize %d:\n\n", $n);

  $r = $p->factorize($n);
  $l1 = $p->numberOfDigits(max(array_column($r, 0)));
  $l2 = $p->numberOfDigits(max(array_column($r, 1)));
  $s = str_repeat(' ', $l1 + 1);
  $b = $s . str_repeat('-', $l2);

  foreach ($r as $f) {
      echo (
          $f[0]
          ? sprintf("%" . $l1 . "d)%" . $l2 . "d\n%s\n", $f[0], $f[1], $b)
          : sprintf("%s%" . $l2 . "d\n", $s, $f[1])
      );
  }
  echo "\n";

  // Factorized formula
  echo $n . " = " . $p->factorizedFormula($n)['formula'] . "\n";
  

  

  occi7\PhpMathInteger\Divisor;

  $d = new Divisor();
  $a = 12;
  $b = 18;

  // Number of divisors
  echo sprintf("%d has %d divisors.\n", $a, $d->count($a));

  // List of all divisors
  echo sprintf("[%s]\n", implode(', ', $d->list($a)));

  // Common factors
  echo sprintf("%d = %s\n", $a, $d->formula($a));
  echo sprintf("%d = %s\n", $b, $d->formula($b));
  echo sprintf(
      "common factors : %s\n",
      $d->formula($d->value($d->commonFactors($a, $b)))
  );
  echo sprintf(
      "common divisors : [%s]\n",
      implode(', ', $d->commonDivisors($a, $b))
  );

  // greatest common factor (divisor)
  echo sprintf(
      "greatest common factor (divisor) : %s\n",
      $d->greatestCommonFactor($a, $b)
  );

  // Reducing fraction
  $r = $d->reduceFraction($a, $b);
  $ra = $d->value($r[0]);
  $rb = $d->value($r[1]);
  echo sprintf("%d/%d reduces to %d/%d\n", $a, $b, $ra, $rb);
  

  

  occi7\PhpMathInteger\Multiple;

  $m = new Multiple();
  $a = 12;
  $b = 18;

  // least common multiple
  echo sprintf(
      "least common multiple of %d and %d is %d\n",
      $a,
      $b,
      $m->leastCommonMultiple($a, $b)
  );
  

  

  occi7\PhpMathInteger\Euclid;

  // Presets values
  $e = new Euclid();
  $a = 390;
  $b = 273;
  $c = 39;

  // Judges if $c is GCD($a, $b) or not
  echo sprintf(
      "Is %d GCD(%d, %d)? - %s.\n",
      $c,
      $a,
      $b,
      $e->isGcdOf($c, $a, $b) ? 'Yes' : 'No'
  );

  // Euclidean Algorithm
  $r = $e->run($a, $b);
  echo "Euclidean Algorithm:\n";
  foreach ($r['processText'] as $t) {
      echo $t . "\n";
  }

  // Formula of remainders
  echo "Remainders can be expressed as:\n";
  foreach ($r['processData'] as $d) {
      echo sprintf("%d = %d - %d * %d\n", $d['r'], $d['a'], $d['b'], $d['c']);
  }

  // Judges if $a and $b are coprime or not
  echo sprintf(
      "Are %d and %d coprime? - %s.\n",
      $a,
      $b,
      $e->isCoprime($a, $b) ? 'Yes' : 'No'
  );

  // GCD($a, $b)
  echo sprintf(
      "Because the Greatest Common Divisor of %d and %d is %d.\n",
      $a,
      $b,
      $e->gcd($a, $b)
  );
  

  

  occi7\PhpMathInteger\Fraction;

  // prest: 1 and 2/4
  $f = new Fraction('1 2/4');

  // is reduced or not?
  echo $f->text() . " is " . ($f->isReduced() ? '' : 'not ') . "reduced.\n";

  // is proper or not?
  echo $f->text() . " is " . ($f->isProper() ? '' : 'not ') . "a proper fraction.\n";

  // is improper or not?
  echo $f->text() . " is " . ($f->isImproper() ? '' : 'not ') . "a improper fraction.\n";

  // is mixed or not?
  echo $f->text() . " is " . ($f->isMixed() ? '' : 'not ') . "a mixed fraction.\n";

  // convert $f into a improper fraction
  echo $f->text() . " = " . $f->improper()->text() . "\n";

  // convert $f into a mixed fraction
  echo $f->text() . " = " . $f->mixed()->text() . "\n";

  // reduce fraction
  echo $f->text() . " reduces to " . $f->reduce()->text() . "\n";

  // integral part
  echo "integral part of " . $f->text() . " is " . $f->int() . "\n";

  // change into a float
  echo $f->text() . " = " . $f->float() . "\n";

  // four arithmetic operations
  $f1 = new Fraction('1/3');
  $f2 = new Fraction('1/6');
  echo $f1->text() . ' + ' . $f2->text() . ' = ' . $f1->add($f2)->text() . "\n";
  $f1->set('2/3');
  $f2->set('1/6');
  echo $f1->text() . ' - ' . $f2->text() . ' = ' . $f1->substract($f2)->text() . "\n";
  $f1->set('2/3');
  $f2->set('1/6');
  echo $f1->text() . ' * ' . $f2->text() . ' = ' . $f1->multiply($f2)->text() . "\n";
  $f1->set('2/3');
  $f2->set('1/6');
  echo $f1->text() . ' / ' . $f2->text() . ' = ' . $f1->divide($f2)->text() . "\n";

  // reduce fractions to a common denominator
  $f1->set('1/3');
  $f2->set('2/5');
  echo "reduce the fractions of " . $f1->text() . " and " . $f2->text()
      . " to a common denominator:\n";
  $f1->toCommonDenominator($f2);
  echo $f1->text() . " and " . $f2->text() . "\n";
  

  

  occi7\PhpMathInteger\Bezout;

  // Bezout's Identity: 3x + 4y = 1
  $b = new Bezout([3, 4, 1, ]);
  echo sprintf("Bezout's Identity: %s\n", $b->identity());

  // Solvable or not
  echo sprintf("Is it solvable? - %s.\n", ($b->isSolvable() ? 'Yes' : 'No'));

  // A solution set
  $s = $b->solution()['solution'];
  echo sprintf("A solutionset: (x, y) = (%d, %d)\n", $s['x'], $s['y']);

  // General solution
  $g = $b->generalSolution()['generalSolution']['formula'];
  echo sprintf("General solution:\n\t%s\n\t%s\n", $g['x'], $g['y']);