Last active
December 30, 2015 16:49
-
-
Save misty320/7857108 to your computer and use it in GitHub Desktop.
釧路の未来を探すプロジェクト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 釧路確認クラス | |
class Kushiro | |
{ | |
public function isKushiro($num) | |
{ | |
if(!$this->isComposite($num)) return false; | |
if(!$this->isTriangle($num)) return false; | |
if(!$this->isHexagonal($num)) return false; | |
return true; | |
} | |
public function isHexagonal($num) | |
{ | |
for($i = 1; $i <= $num; $i++) | |
{ | |
$h = $i*(2*$i -1); | |
if($h == $num) return true; | |
} | |
return false; | |
} | |
public function isTriangle($num) | |
{ | |
for($i = 1; $i <= $num; $i++) | |
{ | |
$t = ($i*($i+1))/2; | |
if($t == $num) return true; | |
} | |
return false; | |
} | |
public function isComposite($num) | |
{ | |
//2未満は合成数ではない | |
if($num < 2) return false; | |
//素数か?の逆 | |
return !$this->isPrime($num); | |
} | |
public function isPrime($num) | |
{ | |
//2未満は素数ではない | |
if($num < 2) return false; | |
//2以上、その数自身未満まで試す | |
for($i=2; $i<$num; $i++) | |
{ | |
//割り切れた場合は素数でない | |
if($num % $i == 0) return false; | |
} | |
return true; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 実行プログラム。946以上の釧路を見つけ出す。 | |
require_once dirname(__FILE__).'/kushiro.php'; | |
$kushiro_explorer = new Kushiro(); | |
for($i=946; $i < 1300; $i++) | |
{ | |
if($kushiro_explorer->isKushiro($i)) | |
{ | |
echo $i." は釧路です\n"; | |
} | |
else | |
{ | |
echo $i."\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//テスト用クラス | |
require_once __DIR__.'/kushiro.php'; | |
class kushiroTest extends PHPUnit_Framework_TestCase | |
{ | |
public function setUp() | |
{ | |
} | |
public function test_釧路か() | |
{ | |
$obj = new Kushiro(); | |
$result = $obj->isKushiro(946); | |
$this->assertTrue($result, '946は釧路'); | |
$result = $obj->isKushiro(1); | |
$this->assertFalse($result, '1は釧路ではない'); | |
$result = $obj->isKushiro(945); | |
$this->assertFalse($result, '945は釧路ではない'); | |
$result = $obj->isKushiro(947); | |
$this->assertFalse($result, '947は釧路ではない'); | |
} | |
public function test_与えられた数が六角数かわかる() | |
{ | |
$obj = new Kushiro(); | |
$result = $obj->isHexagonal(1); | |
$this->assertTrue($result, '1は六角数'); | |
$result = $obj->isHexagonal(6); | |
$this->assertTrue($result, '6は六角数'); | |
$result = $obj->isHexagonal(15); | |
$this->assertTrue($result, '15は六角数'); | |
$result = $obj->isHexagonal(28); | |
$this->assertTrue($result, '28は六角数'); | |
$result = $obj->isHexagonal(2); | |
$this->assertFalse($result, '2は六角数ではない'); | |
$result = $obj->isHexagonal(3); | |
$this->assertFalse($result, '3は六角数ではない'); | |
$result = $obj->isHexagonal(4); | |
$this->assertFalse($result, '4は六角数ではない'); | |
$result = $obj->isHexagonal(5); | |
$this->assertFalse($result, '2は六角数ではない'); | |
$result = $obj->isHexagonal(7); | |
$this->assertFalse($result, '7は六角数ではない'); | |
$result = $obj->isHexagonal(14); | |
$this->assertFalse($result, '14は六角数ではない'); | |
$result = $obj->isHexagonal(16); | |
$this->assertFalse($result, '16は六角数ではない'); | |
$result = $obj->isHexagonal(27); | |
$this->assertFalse($result, '27は六角数ではない'); | |
$result = $obj->isHexagonal(14); | |
$this->assertFalse($result, '29は六角数ではない'); | |
} | |
public function test_与えられた数が三角数かわかる() | |
{ | |
$obj = new Kushiro(); | |
$result = $obj->isTriangle(1); | |
$this->assertTrue($result, '1は三角数'); | |
$result = $obj->isTriangle(2); | |
$this->assertFalse($result, '2は三角数ではない'); | |
$result = $obj->isTriangle(3); | |
$this->assertTrue($result, '3は三角数'); | |
$result = $obj->isTriangle(4); | |
$this->assertFalse($result, '4は三角数ではない'); | |
$result = $obj->isTriangle(5); | |
$this->assertFalse($result, '4は三角数ではない'); | |
$result = $obj->isTriangle(6); | |
$this->assertTrue($result, '6は三角数'); | |
$result = $obj->isTriangle(9); | |
$this->assertFalse($result, '9は三角数ではない'); | |
$result = $obj->isTriangle(10); | |
$this->assertTrue($result, '12は三角数'); | |
} | |
public function test_与えられた数字が合成数か分かる() | |
{ | |
$obj = new Kushiro(); | |
$result = $obj->isComposite(1); | |
$this->assertFalse($result, '1は合成数ではない'); | |
$result = $obj->isComposite(2); | |
$this->assertFalse($result, '2は合成数ではない'); | |
$result = $obj->isComposite(4); | |
$this->assertTrue($result, '4は合成数'); | |
$result = $obj->isComposite(997); | |
$this->assertFalse($result, '997は合成数ではない'); | |
$result = $obj->isComposite(999); | |
$this->assertTrue($result, '999は合成数'); | |
} | |
public function test_与えられた数字が素数か分かる() | |
{ | |
$obj = new Kushiro(); | |
$result = $obj->isPrime(1); | |
$this->assertFalse($result, '1は素数でないのでfalse'); | |
$result = $obj->isPrime(2); | |
$this->assertTrue($result, '2は素数なのでtrue'); | |
$result = $obj->isPrime(4); | |
$this->assertFalse($result, '4は素数なのでfalse'); | |
$result = $obj->isPrime(5); | |
$this->assertTrue($result, '5は素数でないのでtrue'); | |
$result = $obj->isPrime(997); | |
$this->assertTrue($result, '997は素数なのでtrue'); | |
$result = $obj->isPrime(999); | |
$this->assertFalse($result, '999は素数でないのでtrue'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment