function dividedByThree(int $number): bool { $abs = abs($number); if($abs < 10){ return $abs == 3 || $abs == 6 || $abs == 9; } $sum = array_sum(str_split($abs,1)); return dividedByThree($sum); }
- function dividedByThree(int $number): bool
- {
$i = 3;do {if (abs($number) === $i) {return true;}$i += 3;} while (abs($number) >= $i);return false;- $abs = abs($number);
- if($abs < 10){
- return $abs == 3 || $abs == 6 || $abs == 9;
- }
- $sum = array_sum(str_split($abs,1));
- return dividedByThree($sum);
- }
class MyTestCases extends TestCase { public function testDividedByThree() { $this->assertTrue(dividedByThree(3)); $this->assertTrue(dividedByThree(12)); $this->assertFalse(dividedByThree(13)); $this->assertFalse(dividedByThree(0)); $this->assertTrue(dividedByThree(-12)); } public function testRandomNumbers(){ $random_number_array = range(1, 100); shuffle($random_number_array ); foreach($random_number_array as $value){ $isDivisible = $value % 3 == 0; $this->assertEquals($isDivisible,dividedByThree($value)); } } }
- class MyTestCases extends TestCase
- {
- public function testDividedByThree() {
- $this->assertTrue(dividedByThree(3));
- $this->assertTrue(dividedByThree(12));
- $this->assertFalse(dividedByThree(13));
- $this->assertFalse(dividedByThree(0));
- $this->assertTrue(dividedByThree(-12));
- }
- public function testRandomNumbers(){
- $random_number_array = range(1, 100);
- shuffle($random_number_array );
- foreach($random_number_array as $value){
- $isDivisible = $value % 3 == 0;
- $this->assertEquals($isDivisible,dividedByThree($value));
- }
- }
- }