IntegerAxiomsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace MathPHP\Tests\NumberTheory;
  3. use MathPHP\NumberTheory\Integer;
  4. use MathPHP\Algebra;
  5. /**
  6. * Tests of number theory axioms
  7. * These tests don't test specific functions,
  8. * but rather number theory axioms which in term make use of multiple functions.
  9. * If all the number theory math is implemented properly, these tests should
  10. * all work out according to the axioms.
  11. *
  12. * Axioms tested:
  13. * - Coprime
  14. * - lcm(a, b) = ab
  15. * - Prime factorization
  16. * - All primes
  17. */
  18. class IntegerAxiomsTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * Axiom: If a and b are coprime ⇒ lcm(a, b) = ab
  22. * If a and b are coprime, then the least common multiple of a and b is equal to their product ab.
  23. * @dataProvider dataProviderForCoprime
  24. * @param int $a
  25. * @param int $b
  26. */
  27. public function testCoprimeProductEqualsLcm(int $a, int $b)
  28. {
  29. // Given
  30. $ab = $a * $b;
  31. // When
  32. $lcm⟮a、b⟯ = Algebra::lcm($a, $b);
  33. // Then
  34. $this->assertEquals($lcm⟮a、b⟯, $ab);
  35. }
  36. public function dataProviderForCoprime(): array
  37. {
  38. return [
  39. [1, 0],
  40. [1, 2],
  41. [1, 3],
  42. [1, 4],
  43. [1, 5],
  44. [1, 6],
  45. [1, 7],
  46. [1, 8],
  47. [1, 9],
  48. [1, 10],
  49. [1, 20],
  50. [1, 30],
  51. [1, 100],
  52. [2, 3],
  53. [2, 5],
  54. [2, 7],
  55. [2, 9],
  56. [2, 11],
  57. [2, 13],
  58. [2, 15],
  59. [2, 17],
  60. [2, 19],
  61. [2, 21],
  62. [2, 23],
  63. [2, 25],
  64. [2, 27],
  65. [2, 29],
  66. [3, 4],
  67. [3, 5],
  68. [3, 7],
  69. [3, 8],
  70. [3, 10],
  71. [3, 11],
  72. [3, 13],
  73. [3, 14],
  74. [3, 16],
  75. [4, 3],
  76. [4, 5],
  77. [4, 7],
  78. [4, 17],
  79. [4, 21],
  80. [4, 35],
  81. [5, 6],
  82. [5, 7],
  83. [5, 8],
  84. [5, 9],
  85. [5, 11],
  86. [5, 12],
  87. [5, 13],
  88. [5, 14],
  89. [5, 16],
  90. [5, 27],
  91. [6, 7],
  92. [6, 11],
  93. [6, 13],
  94. [6, 17],
  95. [6, 29],
  96. [6, 23],
  97. [6, 25],
  98. [6, 29],
  99. [19, 20],
  100. [20, 21],
  101. [23, 24],
  102. [23, 25],
  103. [27, 16],
  104. [28, 29],
  105. [29, 30],
  106. ];
  107. }
  108. /**
  109. * Axiom: Prime factorization produces only primes
  110. * @return void
  111. */
  112. public function testPrimeFactorizationAllPrimes(): void
  113. {
  114. for ($i = 2; $i < 10000; $i++) {
  115. $primes = Integer::primeFactorization($i);
  116. foreach ($primes as $prime) {
  117. $this->assertTrue(Integer::isPrime($prime));
  118. }
  119. }
  120. }
  121. }