Skip to content

Commit 8d80c99

Browse files
authored
Merge pull request #15 from macocci7/dev_#1
#1: create base functions: done.
2 parents 21895d0 + 1e161e3 commit 8d80c99

16 files changed

Lines changed: 454 additions & 63 deletions

README.md

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@ Math library of Integer:
44

55
PHP-Math-Integer treats the subjects of number theory (only natural number).
66

7+
## Contents
8+
9+
- [Requirements](#requirements)
10+
- [Installation](#installation)
11+
- [Usage](#usage)
12+
- [Functions](#functions)
13+
- [Examples](#examples)
14+
- [LICENSE](#license)
15+
716
## Requirements
817

918
- PHP 8.0.0 (CLI) or later
10-
- Composer
19+
- [Composer](https://getcomposer.org/)
1120

1221
## Installation
1322

@@ -17,24 +26,100 @@ composer require macocci7/php-math-integer
1726

1827
## Usage
1928

29+
See [Examples](#examples).
30+
2031
## Functions
2132

22-
- Number
23-
- Prime
24-
- Divisor
25-
- Multiple
26-
- Euclid (Euclidean Algorithm)
27-
- Fraction
28-
- Bezout (Bezout's Equation)
33+
- Number: treats basic matters of numbers
34+
- isInt(): judges if the param is integer or not
35+
- isIntAll(): judges if all of the param are integer or not
36+
- isNatural(): judges if the param is natural number or not
37+
- isNaturalAll(): judges if all of the param are natural number or not
38+
- isFloat(): judges if the param is float or not
39+
- isFloatAll(): judges if all of the param are float or not
40+
- isNumber(): judges if the param is number or not (different from is_numeric())
41+
- isNumberAll(): judges if all of the param are number or not
42+
- isFraction(): judges if the param is decimal fraction or not
43+
- isFractionAll(): judges if all of the param are fraction or not
44+
- sign(): returns the sign of the param as one of -1, 0 or 1
45+
- int(): returns the integral part of the param
46+
- fraction(): returns the fractional part of the param
47+
- nthDigit(): returns the nth digit of the param
48+
- numberOfDigits(): returns the number of digits of the param
49+
- numberOfFractionalDigits(): returns the number of fractional digits of the param
50+
51+
- Prime: treats matters of primes
52+
- isPrime(): judges if the param is prime or not
53+
- isPrimeAll(): judges if all of the param are prime or not
54+
- next(): returns a prime next to the param
55+
- between(): returns array of primes between the params
56+
- factorize(): factorize the param and returns the process as an array
57+
- factors(): returns the factorized factors of the param as an array
58+
- factorizedFormula(): returns the factorized formula as an array
59+
60+
- Divisor: treats matters of divisors
61+
- count(): returns the number of divisors of the param
62+
- value(): converts the factorized array into an integer and returns it
63+
- formula(): returns the factorized formula as strings
64+
- list(): returns all of divisors of the param as an array
65+
- commonFactors(): returns common factors of the params as an array
66+
- greatestCommonFactor(): returns the greatest common factor of the params
67+
- commonDivisors(): returns all of common divisors of the param as an array
68+
- reduceFraction(): returns reduced fraction consisting of the params as an array
69+
70+
- Multiple: treats matters of multiples
71+
- leastCommonMultiple(): returns the least common multiple of the params
72+
73+
- Euclid (Euclidean Algorithm): treats matters of Euclidean Algorithm
74+
- run(): runs the Euclidean Algorithm and returns the result as an array
75+
- gcd(): returns the greatest common divisor of the params
76+
- isGcdOf(): judges if the first param is gcd of the other params or not
77+
- isCoprime(): judges if the params are coprime or not
78+
79+
- Fraction: treats matters of Fractions
80+
- set(): sets the properties of the fraction specified by the param
81+
- isReduced(): judges if the fraction is reduced or not
82+
- isProper(): judges if the fraction is a proper fraction or not
83+
- isImproper(): judges if the fraction is a improper fraction or not
84+
- isMixed(): judges if the fraction is a mixed fraction or not
85+
- reduce(): reduces the fraction
86+
- toCommonDenominator(): converts the fractions into a common denominator
87+
- add(): adds the param to the fraction
88+
- substruct(): substructs the param from the fraction
89+
- multiply(): multiplies the fraction by the param
90+
- divide(): divide the fraction by the param
91+
- improper(): converts the fraction into a improper fraction
92+
- mixed(): converts the fraction into a mixed fraction
93+
- int(): returns the value of the fraction as an integer
94+
- float(): returns the value of the fraction as an float
95+
- text(): returns the fraction as one-line-text
96+
97+
- Bezout (Bezout's Equation): treats matters of Bezout's Equations
98+
- set(): sets the properties of a Bezout's Equation from the param
99+
- clear(): clears the properties of the Bezout's Equation
100+
- equation(): returns the Bezout's Equation as one-line-text
101+
- isSolvable(): judges if the Bezout's Equation is solvable or not
102+
- solution(): returns a set of solution as an array
103+
- generalSolution(): returns the general solution as an array
104+
105+
## Examples
106+
107+
- [UseNumber.php](example/UseNumber.php) results in >> [UseNumber.txt](example/UseNumber.txt)
108+
- [UsePrime.php](example/UsePrime.php) results in >> [UsePrime.txt](example/UsePrime.txt)
109+
- [UseDivisor.php](example/UseDivisor.php) results in >> [UseDivisor.txt](example/UseDivisor.txt)
110+
- [UseMultiple.php](example/UseMultiple.php) results in >> [UseMultiple.txt](example/UseMultiple.txt)
111+
- [UseFraction.php](example/UseFraction.php) results in >> [UseFraction.txt](example/UseFraction.txt)
112+
- [UseEuclid.php](example/UseEuclid.php) results in >> [UseEuclid.txt](example/UseEuclid.txt)
113+
- [UseBezout.php](example/UseBezout.php) results in >> [UseBezout.txt](example/UseBezout.txt)
29114

30-
## License
115+
## LICENSE
31116

32117
[MIT](LICENSE)
33118

34119
***
35120

36121
*Document Created: 2023/10/19*
37122

38-
*Document Updated: 2023/11/04*
123+
*Document Updated: 2023/11/07*
39124

40125
Copyright 2023 macocci7

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
{
2+
"name": "macocci7/php-math-integer",
3+
"version": "1.0.0",
4+
"description": "PHP Math Library of the subjects of number theory(only natural number).",
5+
"type": "library",
6+
"license": "MIT",
7+
"autoload": {
8+
"psr-4": {
9+
"Macocci7\\PhpMathInteger\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "macocci7",
15+
"email": "macocci7@yahoo.co.jp"
16+
}
17+
],
18+
"minimum-stability": "stable",
219
"require-dev": {
320
"squizlabs/php_codesniffer": "^3.7"
421
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/UseBezout.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* Examples of operation with
5+
* Macocci7\PhpMathInteger\Bezout
6+
*/
7+
8+
require_once('../src/loader.php');
9+
10+
use Macocci7\PhpMathInteger\Bezout;
11+
12+
// Bezout's Equation: 3x + 4y = 1
13+
$b = new Bezout([3, 4, 1, ]);
14+
echo sprintf("Bezout's Equation: %s\n", $b->equation());
15+
16+
// Solvable or not
17+
echo sprintf("Is it solvable? - %s.\n", ($b->isSolvable() ? 'Yes' : 'No'));
18+
19+
// A solution set
20+
$s = $b->solution()['solution'];
21+
echo sprintf("A solutionset: (x, y) = (%d, %d)\n", $s['x'], $s['y']);
22+
23+
// General solution
24+
$g = $b->generalSolution()['generalSolution']['formula'];
25+
echo sprintf("General solution:\n\t%s\n\t%s\n", $g['x'], $g['y']);

example/UseBezout.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Bezout's Equation: 3x + 4y = 1
2+
Is it solvable? - Yes.
3+
A solutionset: (x, y) = (-1, 1)
4+
General solution:
5+
x = 4k - 1
6+
y = 3k + 1

example/UseEuclid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
// Euclidean Algorithm
2828
$r = $e->run($a, $b);
29-
echo "Euclidean Algorrith:\n";
29+
echo "Euclidean Algorithm:\n";
3030
foreach ($r['processText'] as $t) {
3131
echo $t . "\n";
3232
}

example/UseEuclid.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Is 39 GCD(390, 273)? - Yes.
2-
Euclidean Algorrith:
2+
Euclidean Algorithm:
33
390 = 273 * 1 + 117
44
273 = 117 * 2 + 39
55
117 = 39 * 3 + 0

example/UseNumber.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
$n = new Number();
1313

1414
var_dump(
15-
$n->isInt(1),
16-
$n->isIntAll([ 1, 2, 3, ]),
17-
$n->isNatural(1),
18-
$n->isNaturalAll([ 1, 2, 3, ]),
19-
$n->isFloat(1.2),
20-
$n->isFloatAll([ -2.1, 0.0, 1.2, ]),
21-
$n->isNumber(1.2),
22-
$n->isNumberAll([ -2, 0.1, 3, ]),
23-
$n->isFraction(0.1),
24-
$n->isFractionAll([ -0.99, 0.1, 0.99]),
25-
$n->sign(-2.5),
26-
$n->int(3.14),
27-
$n->fraction(3.14),
28-
$n->nthDigit(-3, 123.456),
29-
$n->numberOfDigits(-123.4567),
30-
$n->numberOfFractionalDigits(-12.3456),
15+
$n->isInt(1), // true
16+
$n->isIntAll([ 1, 2, 3, ]), // true
17+
$n->isNatural(1), // true
18+
$n->isNaturalAll([ 1, 2, 3, ]), // true
19+
$n->isFloat(1.2), // true
20+
$n->isFloatAll([ -2.1, 0.0, 1.2, ]), // true
21+
$n->isNumber(1.2), // true
22+
$n->isNumberAll([ -2, 0.1, 3, ]), // true
23+
$n->isFraction(0.1), // true
24+
$n->isFractionAll([ -0.99, 0.1, 0.99]), // true
25+
$n->sign(-2.5), // -1
26+
$n->int(3.14), // 3
27+
$n->fraction(3.14), // 0.1400...
28+
$n->nthDigit(-3, 123.456), // 6
29+
$n->numberOfDigits(-123.4567), // 3
30+
$n->numberOfFractionalDigits(-12.3456), // 4
3131
);

example/UsePrime.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,35 @@
1111

1212
$p = new Prime();
1313

14-
var_dump(
15-
$p->isPrime(3),
16-
$p->isPrimeAll([ 2, 3, 5, ]),
17-
$p->next(5),
18-
$p->between(6, 14),
19-
$p->factors(12), // = 2 * 2 * 3
20-
$p->countSameElements($p->factors(72)), // = (2 ** 3) * (3 ** 2)
14+
// judge if $n is prime or not
15+
$n = 3;
16+
echo sprintf("Is %d prime? - %s.\n", $n, $p->isPrime($n) ? 'Yes' : 'No');
17+
18+
// judge if all of $n are prime or not
19+
$n = [ 2, 3, 5, ];
20+
echo sprintf(
21+
"Are all of [%s] prime? - %s.\n",
22+
implode(', ', $n),
23+
$p->isPrimeAll($n) ? 'Yes' : 'No'
24+
);
25+
26+
// a prime next to $n
27+
$n = 5;
28+
echo sprintf("A prime next to %d is %d.\n", $n, $p->next($n));
29+
30+
// primes between $a and $b
31+
$a = 6;
32+
$b = 14;
33+
echo sprintf(
34+
"Primes between %d and %d are [%s].\n",
35+
$a,
36+
$b,
37+
implode(', ', $p->between($a, $b))
2138
);
2239

2340
// factorize
2441
$n = 1234567890;
42+
echo sprintf("Factorize %d:\n", $n);
2543
$r = $p->factorize($n);
2644
$l1 = $p->numberOfDigits(max(array_column($r, 0)));
2745
$l2 = $p->numberOfDigits(max(array_column($r, 1)));
@@ -35,4 +53,5 @@
3553
);
3654
}
3755

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

example/UsePrime.txt

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
1-
bool(true)
2-
bool(true)
3-
int(7)
4-
array(3) {
5-
[0]=>
6-
int(7)
7-
[1]=>
8-
int(11)
9-
[2]=>
10-
int(13)
11-
}
12-
array(3) {
13-
[0]=>
14-
int(2)
15-
[1]=>
16-
int(2)
17-
[2]=>
18-
int(3)
19-
}
20-
array(2) {
21-
[2]=>
22-
int(3)
23-
[3]=>
24-
int(2)
25-
}
1+
Is 3 prime? - Yes.
2+
Are all of [2, 3, 5] prime? - Yes.
3+
A prime next to 5 is 7.
4+
Primes between 6 and 14 are [7, 11, 13].
5+
Factorize 1234567890:
266
2)1234567890
277
----------
288
3) 617283945

0 commit comments

Comments
 (0)