1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573 |
- <?php
- namespace MathPHP\Tests\NumberTheory;
- use MathPHP\NumberTheory\Integer;
- use MathPHP\Exception;
- class IntegerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @test isPerfectNumber
- * @dataProvider dataProviderForPerfectNumbers
- * @param int $n
- */
- public function testIsPerfectNumber(int $n)
- {
- // When
- $isPerfectNumber = Integer::isPerfectNumber($n);
- // Then
- $this->assertTrue($isPerfectNumber);
- }
- /**
- * @see https://oeis.org/A000396
- * @return array
- */
- public function dataProviderForPerfectNumbers(): array
- {
- return [
- [6],
- [28],
- [496],
- [8128],
- [33550336],
- [8589869056],
- [137438691328],
- ];
- }
- /**
- * @test isPerfectNumber is not a perfect number
- * @dataProvider dataProviderForNonPerfectNumbers
- * @dataProvider dataProviderForAbundantNumbers
- * @dataProvider dataProviderForDeficientNumbers
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- */
- public function testIsNotPerfectNumber(int $n)
- {
- // When
- $isPerfectNumber = Integer::isPerfectNumber($n);
- // Then
- $this->assertFalse($isPerfectNumber);
- }
- /**
- * @return array
- */
- public function dataProviderForNonPerfectNumbers(): array
- {
- return [
- [-1],
- [0],
- [1],
- [2],
- [3],
- [4],
- [5],
- [7],
- [8],
- [9],
- [10],
- [26],
- [498],
- [8124],
- [23550336],
- [2589869056],
- [133438691328],
- ];
- }
- /**
- * @test isAbundantNumber returns true if n is an abundant number
- * @dataProvider dataProviderForAbundantNumbers
- * @param int $n
- * @throws \Exception
- */
- public function testIsAbundantNumber(int $n)
- {
- // When
- $isAbundantNumber = Integer::isAbundantNumber($n);
- // Then
- $this->assertTrue($isAbundantNumber);
- }
- /**
- * A005101 abundant numbers: numbers n such that σ₁(n) > 2n
- * @see https://oeis.org/A005101
- * @return array
- */
- public function dataProviderForAbundantNumbers(): array
- {
- return [
- [12],
- [18],
- [20],
- [24],
- [30],
- [36],
- [40],
- [42],
- [48],
- [54],
- [56],
- [60],
- [66],
- [70],
- [72],
- [78],
- [80],
- [84],
- [88],
- [90],
- [96],
- [100],
- [102],
- [104],
- [270],
- ];
- }
- /**
- * @test isNotAbundantNumber returns true if n is not an abundant number
- * @dataProvider dataProviderForDeficientNumbers
- * @dataProvider dataProviderForPerfectNumbers
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testIsNotAbundantNumber(int $n)
- {
- // When
- $isAbundantNumber = Integer::isAbundantNumber($n);
- // Then
- $this->assertFalse($isAbundantNumber);
- }
- /**
- * @test isDeficientNumber returns true if n is a deficient number
- * @dataProvider dataProviderForDeficientNumbers
- * @param int $n
- * @throws \Exception
- */
- public function testIsDeficientNumber(int $n)
- {
- // When
- $isDeficientNumber = Integer::isDeficientNumber($n);
- // Then
- $this->assertTrue($isDeficientNumber);
- }
- /**
- * A005100 deficient numbers: numbers n such that σ₁(n) < 2n
- * @see https://oeis.org/A005100
- * @return array
- */
- public function dataProviderForDeficientNumbers(): array
- {
- return [
- [1],
- [2],
- [3],
- [4],
- [5],
- [7],
- [8],
- [9],
- [10],
- [11],
- [13],
- [14],
- [15],
- [16],
- [17],
- [19],
- [21],
- [22],
- [23],
- [25],
- [26],
- [27],
- [29],
- [31],
- [32],
- ];
- }
- /**
- * @test isNotDeficientNumber returns true if n is not a deficient number
- * @dataProvider dataProviderForAbundantNumbers
- * @dataProvider dataProviderForPerfectNumbers
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testIsNotDeficientNumber(int $n)
- {
- // When
- $isDeficientNumber = Integer::isDeficientNumber($n);
- // Then
- $this->assertFalse($isDeficientNumber);
- }
- /**
- * @test isRefactorableNumber returns true if n is a refactorable number
- * @dataProvider dataProviderForRefactorableNumbers
- * @param int $n
- * @throws \Exception
- */
- public function testIsRefactorableNumber(int $n)
- {
- // When
- $isRefactorableNumber = Integer::isRefactorableNumber($n);
- // Then
- $this->assertTrue($isRefactorableNumber);
- }
- /**
- * A033950 Refactorable numbers: number of divisors of n divides n. Also known as tau numbers.
- * @see https://oeis.org/A033950
- * @return array
- */
- public function dataProviderForRefactorableNumbers(): array
- {
- return [
- [1],
- [2],
- [8],
- [9],
- [12],
- [18],
- [24],
- [36],
- [40],
- [56],
- [60],
- [72],
- [80],
- [84],
- [88],
- ];
- }
- /**
- * @test isNotRefactorableNumber returns true if n is not a refactorable number
- * @dataProvider dataProviderForNonRefactorableNumbers
- * @param int $n
- * @throws \Exception
- */
- public function testIsNotRefactorableNumber(int $n)
- {
- // When
- $isRefactorableNumber = Integer::isRefactorableNumber($n);
- // Then
- $this->assertFalse($isRefactorableNumber);
- }
- /**
- * @return array
- */
- public function dataProviderForNonRefactorableNumbers(): array
- {
- return [
- [-1],
- [0],
- [3],
- [10],
- [13],
- [17],
- ];
- }
- /**
- * @test testIsSphenicNumber
- * @dataProvider dataProviderForSphenicNumbers
- * @param int $n
- * @throws \Exception
- */
- public function testIsSphenicNumber(int $n)
- {
- // When
- $isSphenicNumber = Integer::isSphenicNumber($n);
- // Then
- $this->assertTrue($isSphenicNumber);
- }
- /**
- * A007304 Sphenic numbers: products of 3 distinct primes
- * @see https://oeis.org/A007304
- * @return array
- */
- public function dataProviderForSphenicNumbers(): array
- {
- return [
- [30],
- [42],
- [66],
- [70],
- [78],
- [102],
- [105],
- [110],
- [114],
- [130],
- [138],
- [154],
- [165],
- [170],
- [174],
- [182],
- [186],
- [190],
- [195],
- ];
- }
- /**
- * @test testIsNotSphenicNumber
- * @dataProvider dataProviderForNonSphenicNumbers
- * @param int $n
- * @throws \Exception
- */
- public function testIsNotSphenicNumber(int $n)
- {
- // When
- $isSphenicNumber = Integer::isSphenicNumber($n);
- // Then
- $this->assertFalse($isSphenicNumber);
- }
- /**
- * @return array
- */
- public function dataProviderForNonSphenicNumbers(): array
- {
- return [
- [2],
- [2 * 3],
- [2 * 2 * 2],
- [2 * 2 * 3 * 5],
- [2 * 3 * 5 * 7],
- ];
- }
- /**
- * @test aliquotSum returns the sum of all proper divisors of n
- * @dataProvider dataProviderForAliquotSums
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testAliquotSum(int $n, int $expected)
- {
- // When
- $actual = Integer::aliquotSum($n);
- // Then
- $this->assertEquals($expected, $actual);
- }
- /**
- * A001065 sum of proper divisors (or aliquot parts) of n
- * @see https://oeis.org/A001065
- * @return array
- */
- public function dataProviderForAliquotSums(): array
- {
- return [
- [1, 0],
- [2, 1],
- [3, 1],
- [4, 3],
- [5, 1],
- [6, 6],
- [7, 1],
- [8, 7],
- [9, 4],
- [10, 8],
- [11, 1],
- [12, 16],
- [13, 1],
- [14, 10],
- [15, 9],
- [16, 15],
- [17, 1],
- [18, 21],
- [19, 1],
- [20, 22],
- [21, 11],
- [22, 14],
- [23, 1],
- [24, 36],
- [25, 6],
- [26, 16],
- [27, 13],
- [28, 28],
- [29, 1],
- [30, 42],
- [31, 1],
- [32, 31],
- [33, 15],
- [34, 20],
- [35, 13],
- [36, 55],
- [2 * 3 * 5 * 7 * 11, 4602],
- ];
- }
- /**
- * @test aliquotSum throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testAliquotSumOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::aliquotSum($n);
- }
- /**
- * @test testRadical
- * @dataProvider dataProviderForRadical
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testRadical(int $n, int $expected)
- {
- // When
- $radical = Integer::radical($n);
- // Then
- $this->assertEquals($expected, $radical);
- }
- /**
- * A007947 the squarefree kernel of n
- * @see https://oeis.org/A007947
- * @return array
- */
- public function dataProviderForRadical(): array
- {
- return [
- [1, 1],
- [2, 2],
- [3, 3],
- [4, 2],
- [5, 5],
- [6, 6],
- [7, 7],
- [8, 2],
- [9, 3],
- [10, 10],
- [11, 11],
- [12, 6],
- [13, 13],
- [14, 14],
- [15, 15],
- [16, 2],
- [17, 17],
- [18, 6],
- [19, 19],
- ];
- }
- /**
- * @test radical throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testRadicalOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::radical($n);
- }
- /**
- * @test testTotient
- * @dataProvider dataProviderForTotient
- * @param int $n
- * @param int $k
- * @param int $expected
- * @throws \Exception
- */
- public function testTotient(int $n, int $k, int $expected)
- {
- // When
- $totient = Integer::totient($n, $k);
- // Then
- $this->assertEquals($expected, $totient);
- }
- /**
- * @see https://oeis.org/A000010 (k=1)
- * @see https://oeis.org/A007434 (k=2)
- * @see https://oeis.org/A059376 (k=3)
- * @see https://oeis.org/A059377 (k=4)
- * @see https://oeis.org/A059378 (k=5)
- * @return array
- */
- public function dataProviderForTotient(): array
- {
- return [
- [1, 1, 1],
- [2, 1, 1],
- [3, 1, 2],
- [4, 1, 2],
- [5, 1, 4],
- [6, 1, 2],
- [7, 1, 6],
- [8, 1, 4],
- [9, 1, 6],
- [10, 1, 4],
- [11, 1, 10],
- [12, 1, 4],
- [13, 1, 12],
- [14, 1, 6],
- [15, 1, 8],
- [16, 1, 8],
- [17, 1, 16],
- [18, 1, 6],
- [1, 2, 1],
- [2, 2, 3],
- [3, 2, 8],
- [4, 2, 12],
- [5, 2, 24],
- [6, 2, 24],
- [7, 2, 48],
- [8, 2, 48],
- [9, 2, 72],
- [10, 2, 72],
- [1, 3, 1],
- [2, 3, 7],
- [3, 3, 26],
- [4, 3, 56],
- [5, 3, 124],
- [6, 3, 182],
- [7, 3, 342],
- [8, 3, 448],
- [9, 3, 702],
- [10, 3, 868],
- ];
- }
- /**
- * @test totient throws an OutOfBoundsException if n is < 1 or k is < 1.
- * @dataProvider dataProviderForTotientOutOfBoundsException
- * @param int $n
- * @param int $k
- * @throws \Exception
- */
- public function testTotientOutOfBoundsException(int $n, int $k)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::totient($n, $k);
- }
- /**
- * @return array
- */
- public function dataProviderForTotientOutOfBoundsException(): array
- {
- return [
- [2, -1],
- [2, 0],
- [0, 0],
- [0, 1],
- [-1, -1],
- [-1, 1],
- [-2, 1],
- [-100, 1],
- [-98352299832, 1],
- ];
- }
- /**
- * @test testCototient
- * @dataProvider dataProviderForCototient
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testCototient(int $n, int $expected)
- {
- // When
- $cototient = Integer::cototient($n);
- // Then
- $this->assertEquals($expected, $cototient);
- }
- /**
- * A051953 n - φ(n)
- * @see https://oeis.org/A051953
- * @return array
- */
- public function dataProviderForCototient(): array
- {
- return [
- [1, 0],
- [2, 1],
- [3, 1],
- [4, 2],
- [5, 1],
- [6, 4],
- [7, 1],
- [8, 4],
- [9, 3],
- [10, 6],
- [11, 1],
- [12, 8],
- [13, 1],
- [14, 8],
- [15, 7],
- [16, 8],
- [17, 1],
- [18, 12],
- [19, 1],
- [20, 12],
- [80, 48],
- ];
- }
- /**
- * @test cototient throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testCototientOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::cototient($n);
- }
- /**
- * @test testReducedTotient
- * @dataProvider dataProviderForReducedTotient
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testReducedTotient(int $n, int $expected)
- {
- // When
- $result = Integer::reducedTotient($n);
- // Then
- $this->assertEquals($expected, $result);
- }
- /**
- * @see https://oeis.org/A002322
- * @return array
- */
- public function dataProviderForReducedTotient(): array
- {
- return [
- [1, 1],
- [2, 1],
- [3, 2],
- [4, 2],
- [5, 4],
- [6, 2],
- [7, 6],
- [8, 2],
- [9, 6],
- [10, 4],
- [11, 10],
- [12, 2],
- [13, 12],
- [14, 6],
- [15, 4],
- [16, 4],
- [17, 16],
- [18, 6],
- [19, 18],
- [64, 16],
- [80, 4],
- [81, 54],
- ];
- }
- /**
- * @test reducedTotient throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testReducedTotientOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::reducedTotient($n);
- }
- /**
- * @test testMobius
- * @dataProvider dataProviderForMobius
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testMobius(int $n, int $expected)
- {
- // When
- $actual = Integer::mobius($n);
- // Then
- $this->assertEquals($expected, $actual);
- }
- /**
- * A008683
- * @see https://oeis.org/A008683
- * @return array
- */
- public function dataProviderForMobius(): array
- {
- return [
- [1, 1],
- [2, -1],
- [3, -1],
- [4, 0],
- [5, -1],
- [6, 1],
- [7, -1],
- [8, 0],
- [9, 0],
- [10, 1],
- [11, -1],
- [12, 0],
- [13, -1],
- [14, 1],
- [15, 1],
- ];
- }
- /**
- * @test mobius throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testMobiusOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::mobius($n);
- }
- /**
- * @test testIsSquarefree
- * @dataProvider dataProviderForSquarefreeIntegers
- * @param int $n
- * @throws \Exception
- */
- public function testIsSquarefree(int $n)
- {
- // When
- $isSquarefree = Integer::isSquarefree($n);
- // Then
- $this->assertTrue($isSquarefree);
- }
- /**
- * A005117 squarefree numbers: numbers that are not divisible by a square greater than 1
- * @see https://oeis.org/A005117
- * @return array
- */
- public function dataProviderForSquarefreeIntegers(): array
- {
- return [
- [1],
- [2],
- [3],
- [5],
- [6],
- [7],
- [10],
- [11],
- [13],
- [14],
- [15],
- [17],
- [19],
- [21],
- [22],
- [23],
- [26],
- [29],
- [30],
- [31],
- ];
- }
- /**
- * @test testIsNotSquarefree
- * @dataProvider dataProviderForNonSquarefreeIntegers
- * @param int $n
- * @throws \Exception
- */
- public function testIsNotSquarefree(int $n)
- {
- // When
- $isSquarefree = Integer::isSquarefree($n);
- // Then
- $this->assertFalse($isSquarefree);
- }
- /**
- * @return array
- */
- public function dataProviderForNonSquarefreeIntegers(): array
- {
- return [
- [-1],
- [0],
- [2 * 2],
- [2 * 2 * 2],
- [2 * 3 * 3],
- [2 * 3 * 5 * 7 * 11 * 13 * 17 * 17],
- ];
- }
- /**
- * @test testSumOfDivisors
- * @dataProvider dataProviderForSumOfDivisors
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testSumOfDivisors(int $n, int $expected)
- {
- // When
- $actual = Integer::sumOfDivisors($n);
- // Then
- $this->assertEquals($expected, $actual);
- }
- /**
- * A000203 the sum of the divisors of n
- * @see https://oeis.org/A000203
- * @return array
- */
- public function dataProviderForSumOfDivisors(): array
- {
- return [
- [1, 1],
- [2, 3],
- [3, 4],
- [4, 7],
- [5, 6],
- [6, 12],
- [7, 8],
- [8, 15],
- [9, 13],
- [10, 18],
- [11, 12],
- [12, 28],
- [13, 14],
- [14, 24],
- [15, 24],
- [70, 144],
- [44100, 160797],
- ];
- }
- /**
- * @test sumOfDivisors throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testSumOfDivisorsOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::sumOfDivisors($n);
- }
- /**
- * @test testNumberOfDivisors
- * @dataProvider dataProviderForNumberOfDivisors
- * @param int $n
- * @param int $expected
- * @throws \Exception
- */
- public function testNumberOfDivisors(int $n, int $expected)
- {
- // When
- $actual = Integer::numberOfDivisors($n);
- // Then
- $this->assertEquals($expected, $actual);
- }
- /**
- * A000005 the numbers of divisors of n
- * @see https://oeis.org/A000005
- * @return array
- */
- public function dataProviderForNumberOfDivisors(): array
- {
- return [
- [1, 1],
- [2, 2],
- [3, 2],
- [4, 3],
- [5, 2],
- [6, 4],
- [7, 2],
- [8, 4],
- [9, 3],
- [10, 4],
- [96, 12],
- [103, 2],
- ];
- }
- /**
- * @test numberOfDivisors throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testNumberOfDivisorsOutOfBoundsException(int $n)
- {
- // When
- $this->expectException(Exception\OutOfBoundsException::class);
- // Then
- Integer::numberOfDivisors($n);
- }
- /**
- * @test isPerfectPower returns true if n is a perfect prime.
- * @dataProvider dataProviderForIsPerfectPower
- * @param int $n
- */
- public function testIsPerfectPower(int $n)
- {
- // When
- $isPerfectPower = Integer::isPerfectPower($n);
- // Then
- $this->assertTrue($isPerfectPower);
- }
- /**
- * A001597 Perfect powers: m^k where m > 0 and k >= 2.
- * @see https://oeis.org/A001597
- * @return array
- */
- public function dataProviderForIsPerfectPower(): array
- {
- return [
- [4],
- [8],
- [9],
- [16],
- [16],
- [25],
- [27],
- [32],
- [36],
- [49],
- [64],
- [64],
- [64],
- [81],
- [81],
- [100],
- [121],
- [125],
- [128],
- [144],
- [169],
- [196],
- [216],
- [225],
- [243],
- [256],
- [256],
- [256],
- [289],
- [324],
- [343],
- [361],
- [400],
- [441],
- [484],
- [512],
- [512],
- [529],
- [576],
- [625],
- [625],
- [676],
- [729],
- [729],
- [729],
- [784],
- [841],
- [900],
- [961],
- [1000],
- [1024],
- [1024],
- [1024],
- [1089],
- ];
- }
- /**
- * @test isPerfectPower returns false if n is not a perfect prime.
- * @dataProvider dataProviderForIsNotPerfectPower
- * @param int $n
- */
- public function testIsNotPerfectPower(int $n)
- {
- // When
- $isPerfectPower = Integer::isPerfectPower($n);
- // Then
- $this->assertFalse($isPerfectPower);
- }
- /**
- * A007916 Numbers that are not perfect powers.
- * @see https://oeis.org/A007916
- * @return array
- */
- public function dataProviderForIsNotPerfectPower(): array
- {
- return [
- [2],
- [3],
- [5],
- [6],
- [7],
- [10],
- [11],
- [12],
- [13],
- [14],
- [15],
- [17],
- [18],
- [19],
- [20],
- [21],
- [22],
- [23],
- [24],
- [26],
- [28],
- [29],
- [30],
- [31],
- [33],
- [34],
- [35],
- [37],
- [38],
- [39],
- [40],
- [41],
- [42],
- [43],
- [44],
- [45],
- [46],
- [47],
- [48],
- [50],
- [51],
- [52],
- [53],
- [54],
- [55],
- [56],
- [57],
- [58],
- [59],
- [60],
- [61],
- [62],
- [63],
- [65],
- [66],
- [67],
- [68],
- [69],
- [70],
- [71],
- [72],
- [73],
- [74],
- [75],
- [76],
- [77],
- [78],
- [79],
- [80],
- [82],
- [83],
- ];
- }
- /**
- * @test perfectPower returns m and k for n such that mᵏ = n if n is a perfect power.
- * @dataProvider dataProviderForPerfectPower
- * @param int $n
- * @param int $expected_m
- * @param int $expected_k
- */
- public function testPerfectPower(int $n, int $expected_m, int $expected_k)
- {
- // When
- [$m, $k] = Integer::perfectPower($n);
- // Then
- $this->assertEquals($expected_m, $m);
- $this->assertEquals($expected_k, $k);
- }
- /**
- * Perfect powers: m^k where m > 0 and k >= 2.
- * @return array
- */
- public function dataProviderForPerfectPower(): array
- {
- return [
- [4, 2, 2],
- [8, 2, 3],
- [9, 3, 2],
- [16, 2, 4],
- [25, 5, 2],
- [27, 3, 3],
- [32, 2, 5],
- [36, 6, 2],
- [49, 7, 2],
- [64, 2, 6],
- [81, 3, 4],
- [100, 10, 2],
- [121, 11, 2],
- [125, 5, 3],
- [128, 2, 7],
- [144, 12, 2],
- [169, 13, 2],
- [196, 14, 2],
- [216, 6, 3],
- [225, 15, 2],
- [243, 3, 5],
- [256, 2, 8],
- [1000, 10, 3],
- [1024, 2, 10],
- ];
- }
- /**
- * @test perfectPower returns a non-empty array comtaining numeric m and k both > 1 if n is a perfect power.
- * @dataProvider dataProviderForIsPerfectPower
- * @param int $n
- */
- public function testPerfectPowerArray(int $n)
- {
- // When
- $perfect_power = Integer::perfectPower($n);
- // Then
- $this->assertNotEmpty($perfect_power);
- // And
- $m = \array_shift($perfect_power);
- $k = \array_shift($perfect_power);
- $this->assertTrue(\is_numeric($m));
- $this->assertTrue(\is_numeric($k));
- $this->assertGreaterThan(1, $m);
- $this->assertGreaterThan(1, $k);
- }
- /**
- * @test perfectPower returns an empty array if n is not a perfect power.
- * @dataProvider dataProviderForIsNotPerfectPower
- * @param int $n
- */
- public function testEmptyPerfectPower(int $n)
- {
- // When
- $empty = Integer::perfectPower($n);
- // Then
- $this->assertEmpty($empty);
- }
- /**
- * @test primeFactorization returns an array of the prime factors of an integer n.
- * @dataProvider dataProviderForPrimeFactorization
- * @param int $n
- * @param array $expected_actors
- * @throws \Exception
- */
- public function testPrimeFactorization(int $n, array $expected_actors)
- {
- // When
- $factors = Integer::primeFactorization($n);
- // Then
- $this->assertEquals($expected_actors, $factors);
- }
- /**
- * @return array
- */
- public function dataProviderForPrimeFactorization(): array
- {
- return [
- [1, []],
- [2, [2]],
- [3, [3]],
- [4, [2, 2]],
- [5, [5]],
- [6, [2, 3]],
- [7, [7]],
- [8, [2, 2, 2]],
- [9, [3, 3]],
- [10, [2, 5]],
- [11, [11]],
- [12, [2, 2, 3]],
- [13, [13]],
- [14, [2, 7]],
- [15, [3, 5]],
- [16, [2, 2, 2, 2]],
- [17, [17]],
- [18, [2, 3, 3]],
- [19, [19]],
- [20, [2, 2, 5]],
- [48, [2, 2, 2, 2, 3]],
- [99, [3, 3, 11]],
- [100, [2, 2, 5, 5]],
- [101, [101]],
- [111, [3, 37]],
- [147, [3, 7, 7]],
- [200, [2, 2, 2, 5, 5]],
- [5555, [5, 11, 101]],
- [8463, [3, 7, 13, 31]],
- [12345, [3, 5, 823]],
- [45123, [3, 13, 13, 89]],
- [99999, [3, 3, 41, 271]],
- [5465432, [2, 2, 2, 7, 17, 5741]],
- [25794349, [7, 619, 5953]],
- [87534987, [3, 23, 1268623]],
- [123456789, [3, 3, 3607, 3803]],
- [8654893156, [2, 2, 7, 13, 157, 269, 563]],
- [2 * 3 * 5 * 7, [2, 3, 5, 7]],
- [2 * 2 * 2 * 5 * 7 * 7 * 7 * 13, [2, 2, 2, 5, 7, 7, 7, 13]],
- ];
- }
- /**
- * @test primeFactorization throws an OutOfBoundsException if n is < 1.
- * @dataProvider dataProviderForPrimeFactorizationOutOfBoundsException
- * @param int $n
- * @throws \Exception
- */
- public function testPrimeFactorizationOutOfBoundsException(int $n)
- {
- // Then
- $this->expectException(Exception\OutOfBoundsException::class);
- // When
- Integer::primeFactorization($n);
- }
- /**
- * @return array
- */
- public function dataProviderForPrimeFactorizationOutOfBoundsException(): array
- {
- return [
- [0],
- [-1],
- [-2],
- [-100],
- [-98352299832],
- ];
- }
- /**
- * @test coprime returns true if a and b are coprime
- * @dataProvider dataProviderForCoprime
- * @param int $a
- * @param int $b
- */
- public function testCoprime(int $a, int $b)
- {
- // When
- $coprime = Integer::coprime($a, $b);
- // Then
- $this->assertTrue($coprime);
- }
- /**
- * @return array
- */
- public function dataProviderForCoprime(): array
- {
- return [
- [1, 0],
- [-1, 1],
- [1, 2],
- [1, 3],
- [1, 4],
- [1, 5],
- [1, 6],
- [1, 7],
- [1, 8],
- [1, 9],
- [1, 10],
- [1, 20],
- [1, 30],
- [1, 100],
- [2, 3],
- [2, 5],
- [2, 7],
- [2, 9],
- [2, 11],
- [2, 13],
- [2, 15],
- [2, 17],
- [2, 19],
- [2, 21],
- [2, 23],
- [2, 25],
- [2, 27],
- [2, 29],
- [3, 4],
- [3, 5],
- [3, 7],
- [3, 8],
- [3, 10],
- [3, 11],
- [3, 13],
- [3, 14],
- [3, 16],
- [4, 3],
- [4, 5],
- [4, 7],
- [4, 17],
- [4, 21],
- [4, 35],
- [5, 6],
- [5, 7],
- [5, 8],
- [5, 9],
- [5, 11],
- [5, 12],
- [5, 13],
- [5, 14],
- [5, 16],
- [5, 27],
- [6, 7],
- [6, 11],
- [6, 13],
- [6, 17],
- [6, 29],
- [6, 23],
- [6, 25],
- [6, 29],
- [19, 20],
- [20, 21],
- [23, 24],
- [23, 25],
- [27, 16],
- [28, 29],
- [29, 30],
- ];
- }
- /**
- * @test coprime returns false if a and b are not coprime
- * @dataProvider dataProviderForNotCoprime
- * @param int $a
- * @param int $b
- */
- public function testNotCoprime(int $a, int $b)
- {
- // When
- $coprime = Integer::coprime($a, $b);
- // Then
- $this->assertFalse($coprime);
- }
- /**
- * @return array
- */
- public function dataProviderForNotCoprime(): array
- {
- return [
- [2, 4],
- [2, 6],
- [2, 8],
- [2, 10],
- [2, 12],
- [2, 14],
- [2, 16],
- [2, 18],
- [2, 20],
- [2, 22],
- [2, 24],
- [2, 26],
- [2, 28],
- [2, 30],
- [3, 6],
- [3, 9],
- [3, 12],
- [3, 15],
- [4, 8],
- [4, 12],
- [4, 20],
- [4, 22],
- [4, 24],
- [4, 30],
- [5, 10],
- [5, 15],
- [5, 20],
- [5, 25],
- [5, 30],
- [5, 50],
- [5, 100],
- [5, 200],
- [5, 225],
- [5, 555],
- [6, 12],
- [6, 14],
- [6, 16],
- [6, 18],
- [6, 26],
- [6, 28],
- [6, 30],
- [6, 32],
- [12, 21],
- [18, 20],
- [20, 22],
- [21, 24],
- ];
- }
- /**
- * @test isOdd returns true for an odd number
- * @dataProvider dataProviderForOddNumbers
- * @param int $x
- */
- public function testIsOdd(int $x)
- {
- // When
- $isOdd = Integer::isOdd($x);
- // Then
- $this->assertTrue($isOdd);
- }
- /**
- * @test isOdd returns false for an even number
- * @dataProvider dataProviderForEvenNumbers
- * @param int $x
- */
- public function testIsNotOdd(int $x)
- {
- // When
- $isOdd = Integer::isOdd($x);
- // Then
- $this->assertFalse($isOdd);
- }
- /**
- * @test isEven returns true for an even number
- * @dataProvider dataProviderForEvenNumbers
- * @param int $x
- */
- public function testIsEven(int $x)
- {
- // When
- $isEven = Integer::isEven($x);
- // Then
- $this->assertTrue($isEven);
- }
- /**
- * @test isEven returns false for an odd number
- * @dataProvider dataProviderForOddNumbers
- * @param int $x
- */
- public function testIsNotEven(int $x)
- {
- // When
- $isEven = Integer::isEven($x);
- // Then
- $this->assertFalse($isEven);
- }
- /**
- * @return \Generator
- */
- public function dataProviderForOddNumbers(): \Generator
- {
- foreach (\range(-11, 101, 2) as $x) {
- yield [$x];
- }
- }
- /**
- * @return \Generator
- */
- public function dataProviderForEvenNumbers(): \Generator
- {
- foreach (\range(-10, 100, 2) as $x) {
- yield [$x];
- }
- }
- }
|