Basic.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace MathPHP\Sequence;
  3. use MathPHP\Arithmetic;
  4. use MathPHP\Exception;
  5. /**
  6. * Basic integer sequences
  7. * - Arithmetic progression
  8. * - Geometric progression
  9. * - Square numbers
  10. * - Cubic numbers
  11. * - Powers of 2
  12. * - Powers of 10
  13. * - Factorial
  14. *
  15. * All sequences return an array of numbers in the sequence.
  16. * The array index starting point depends on the sequence type.
  17. */
  18. class Basic
  19. {
  20. /**
  21. * Arithmetic progression
  22. * A sequence of numbers such that the difference between the consecutive terms is constant.
  23. * https://en.wikipedia.org/wiki/Arithmetic_progression
  24. *
  25. * Example:
  26. * n = 10
  27. * d = 2
  28. * a₁ = 1
  29. * Sequence: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
  30. * Array index: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  31. *
  32. * @param int $n How many numbers in the sequence
  33. * @param int $d Difference between the elements of the sequence
  34. * @param int $a₁ Starting number for the sequence
  35. *
  36. * @return array<int, int> Indexed from 1
  37. */
  38. public static function arithmeticProgression(int $n, int $d, int $a₁): array
  39. {
  40. if ($n <= 0) {
  41. return [];
  42. }
  43. $progression[1] = $a₁;
  44. for ($i = 1; $i < $n; $i++) {
  45. $progression[$i + 1] = $progression[$i] + $d;
  46. }
  47. return $progression;
  48. }
  49. /**
  50. * Geometric progression
  51. * A sequence of numbers where each term after the first is found by multiplying
  52. * the previous one by a fixed, non-zero number called the common ratio.
  53. * https://en.wikipedia.org/wiki/Geometric_progression
  54. *
  55. * an = arⁿ⁻¹
  56. *
  57. * Example:
  58. * n = 4
  59. * a = 2
  60. * r = 3
  61. * Sequence: 2(3)⁰, 2(3)¹, 2(3)², 2(3)³
  62. * Sequence: 2, 6, 18, 54
  63. * Array index: 0 1 2 3
  64. *
  65. * @param int $n How many numbers in the sequence
  66. * @param int|float $a Scalar value
  67. * @param int|float $r Common ratio
  68. *
  69. * @return array<int|float> Indexed from 0 (indexes are powers of common ratio)
  70. *
  71. * @throws Exception\BadParameterException
  72. */
  73. public static function geometricProgression(int $n, $a, $r): array
  74. {
  75. if ($r === 0) {
  76. throw new Exception\BadParameterException('Common ratio r cannot be 0');
  77. }
  78. $progression = [];
  79. if ($n < 0) {
  80. return $progression;
  81. }
  82. for ($i = 0; $i < $n; $i++) {
  83. $progression[] = $a * $r ** $i;
  84. }
  85. return $progression;
  86. }
  87. /**
  88. * Square numbers
  89. * https://en.wikipedia.org/wiki/Square_number
  90. *
  91. * n²
  92. *
  93. * Example:
  94. * n = 5
  95. * Sequence: 0², 1², 2², 3², 4²
  96. * Sequence: 0, 1, 4, 9, 16
  97. * Array index: 0, 1, 2, 3, 4
  98. *
  99. * @param int $n How many numbers in the sequence
  100. *
  101. * @return array<int> Indexed from 0 (indexes are the base number which is raised to the power of 2)
  102. */
  103. public static function squareNumber(int $n): array
  104. {
  105. $squares = [];
  106. if ($n <= 0) {
  107. return $squares;
  108. }
  109. for ($i = 0; $i < $n; $i++) {
  110. $squares[] = $i ** 2;
  111. }
  112. return $squares;
  113. }
  114. /**
  115. * Cubic numbers
  116. * https://en.wikipedia.org/wiki/Cube_(algebra)
  117. *
  118. * n³
  119. *
  120. * Example:
  121. * n = 5
  122. * Sequence: 0³, 1³, 2³, 3³, 4³
  123. * Sequence: 0, 1, 8, 27, 64
  124. * Array index: 0, 1, 2, 3, 4
  125. *
  126. * @param int $n How many numbers in the sequence
  127. *
  128. * @return array<int> Indexed from 0 (indexes are the base number which is raised to the power of 3)
  129. */
  130. public static function cubicNumber(int $n): array
  131. {
  132. $cubes = [];
  133. if ($n <= 0) {
  134. return $cubes;
  135. }
  136. for ($i = 0; $i < $n; $i++) {
  137. $cubes[] = $i ** 3;
  138. }
  139. return $cubes;
  140. }
  141. /**
  142. * Powers of two
  143. * https://en.wikipedia.org/wiki/Power_of_two
  144. *
  145. * 2ⁿ
  146. *
  147. * Example:
  148. * n = 5
  149. * Sequence: 2⁰, 2¹, 2², 2³, 2⁴
  150. * Sequence: 1, 2, 4, 8, 16
  151. * Array index: 0, 1, 2, 3, 4
  152. *
  153. * @param int $n How many numbers in the sequence
  154. *
  155. * @return array<int> Indexed from 0 (indexes are the power 2 is raised to)
  156. */
  157. public static function powersOfTwo(int $n): array
  158. {
  159. $powers_of_2 = [];
  160. if ($n <= 0) {
  161. return $powers_of_2;
  162. }
  163. for ($i = 0; $i < $n; $i++) {
  164. $powers_of_2[] = 2 ** $i;
  165. }
  166. return $powers_of_2;
  167. }
  168. /**
  169. * Powers of ten
  170. * https://en.wikipedia.org/wiki/Power_of_10
  171. *
  172. * Example:
  173. * n = 5
  174. * Sequence: 10⁰, 10¹, 10², 10³, 10⁴
  175. * Sequence: 1, 10, 100, 1000, 10000
  176. * Array index: 0, 1, 2, 3, 4
  177. *
  178. * @param int $n How many numbers in the sequence
  179. *
  180. * @return array<int> Indexed from 0 (indexes are the power 10 is raised to)
  181. */
  182. public static function powersOfTen(int $n): array
  183. {
  184. $powers_of_10 = [];
  185. if ($n <= 0) {
  186. return $powers_of_10;
  187. }
  188. for ($i = 0; $i < $n; $i++) {
  189. $powers_of_10[] = 10 ** $i;
  190. }
  191. return $powers_of_10;
  192. }
  193. /**
  194. * Factorial
  195. * https://en.wikipedia.org/wiki/Factorial
  196. *
  197. * Example:
  198. * n = 5
  199. * Sequence: 0!, 1!, 2!, 3!, 4!
  200. * Sequence: 1, 1, 2, 6, 24
  201. * Array index: 0, 1, 2, 3, 4
  202. *
  203. * @param int $n How many numbers in the sequence
  204. *
  205. * @return array<int> Indexed from 0 (indexes are the n!)
  206. */
  207. public static function factorial(int $n): array
  208. {
  209. if ($n <= 0) {
  210. return [];
  211. }
  212. $factorial = [1];
  213. if ($n === 1) {
  214. return $factorial;
  215. }
  216. for ($i = 1; $i < $n; $i++) {
  217. $factorial[] = $i * $factorial[$i - 1];
  218. }
  219. return $factorial;
  220. }
  221. /**
  222. * Digit sum (sum of digits)
  223. * https://en.wikipedia.org/wiki/Digit_sum
  224. * https://oeis.org/A007953
  225. *
  226. * Example
  227. * n = 11
  228. * Sequence: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1
  229. * Array index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1
  230. *
  231. * @param int $n How many numbers in the sequence
  232. *
  233. * @return array<int> Indexed from 0 (indexes are the n in the digitSum(n))
  234. */
  235. public static function digitSum(int $n): array
  236. {
  237. if ($n <= 0) {
  238. return [];
  239. }
  240. $digit_sums = [];
  241. for ($i = 0; $i < $n; $i++) {
  242. $digit_sums[] = Arithmetic::digitSum($i);
  243. }
  244. return $digit_sums;
  245. }
  246. /**
  247. * Digital root (iterated digit sum, repeated digital sum)
  248. * https://en.wikipedia.org/wiki/Digital_root
  249. * http://oeis.org/A010888
  250. *
  251. * Example
  252. * n = 11
  253. * Sequence: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1
  254. * Array index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1
  255. *
  256. * @param int $n How many numbers in the sequence
  257. *
  258. * @return array<int> Indexed from 0 (indexes are the n in the digitSum(n))
  259. */
  260. public static function digitalRoot(int $n): array
  261. {
  262. if ($n <= 0) {
  263. return [];
  264. }
  265. $digital_roots = [];
  266. for ($i = 0; $i < $n; $i++) {
  267. $digital_roots[] = Arithmetic::digitalRoot($i);
  268. }
  269. return $digital_roots;
  270. }
  271. }