Skip to content

Instantly share code, notes, and snippets.

@kazua
kazua / array_reduce.php
Created November 27, 2013 14:40
PHPでreduceLeft,reduceRight
<?php
//array_reduceLeftとarray_reduceRight
//write kazua
//array_reduceLeft
//$a:array 入力の配列
//$f:callback => mixed callback ( mixed &$result , mixed $item )
//$i イニシャル値
function array_reduceLeft(array $a, callable $f, $i = null) {
$r = $i;
@kazua
kazua / problem008.php
Last active December 29, 2015 06:19
Project Euler Problem 8(PHP)
<?php
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%208
//write kazua
function problem008() {
for ($a = 1; $a < 1000; $a++)
for ($b = $a + 1; $b < 1000 - $a; $b++)
if (pow($a, 2) + pow($b, 2) == pow(1000 - $a - $b, 2))
return ($a * $b * (1000 - $a - $b));
}
@kazua
kazua / problem113.scala
Created November 21, 2013 14:50
Project Euler Problem 113
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20113
//write kazua
import scala.math._
object problem113 {
def problem113d(n : Int, k : Int, i : Int = 1, r : BigInt = BigInt(0)) : BigInt = r match {
case r if r == BigInt(0) => problem113d(n, min(k, n - k), i, BigInt(1))
case _ => {
i match {
@kazua
kazua / problem112another.scala
Created November 21, 2013 11:37
Project Euler Problem 112(別バージョン)
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20112
//write kazua
object problem112another {
def ckBouncy(n : Int) = n.toString.toList.sortWith(_ < _).mkString.toInt != n && n.toString.toList.sortWith(_ > _).mkString.toInt != n
def problem112(i : Int = 538, b : Int = 269) : Int = (b.toDouble / i.toDouble * 100).toInt match {
case n if n < 99 => if (ckBouncy(i)) problem112(i + 1, b + 1) else problem112(i + 1, b)
case n => i
}
def main(args : Array[String]) {
@kazua
kazua / problem112.php
Created November 19, 2013 21:08
Project Euler Problem 112(PHP)
<?php
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20112
//write kazua
function ckBouncy($n) {
if (strspn($n, "0123456789") != strlen($n)) return false;
$k = $z = $g = str_split($n);
sort($z);
rsort($g);
$r = ($k != $z) && ($k != $g);
unset($k,$z,$g);
@kazua
kazua / problem112.scala
Created November 15, 2013 21:27
Project Euler Problem 112
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20112
//write kazua
object problem112 {
def ckBouncy(n : Int, k : Int = -1, z : Boolean = false, g : Boolean = false) : Boolean = k match {
case k if k == 0 => (z && g)
case k if k < 0 => ckBouncy(n % 10, n / 10)
case k => {
val x = k % 10
if (x < n) ckBouncy(x, k / 10, true, g)
@kazua
kazua / problem030.php
Created November 13, 2013 12:51
Project Euler Problem 30(PHP)
<?php
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2030
//write kazua
for ($i = 2; $i < pow(9, 5) * 5; $i++)
if (array_sum(
array_map(
function ($value) {
return pow($value, 5);
}, str_split($i))) === $i)
$a[] = $i;
@kazua
kazua / problem120.scala
Created November 12, 2013 13:04
Project Euler Problem 120
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%20120
//write kazua
object problem120 {
def problem120 = (3 to 1000).map(i => 2 * i * ((i - 1) / 2)).sum
def main(args : Array[String]) {
println(problem120)
}
}
@kazua
kazua / problem025.php
Created November 10, 2013 05:04
Project Euler Problem 25(PHP)
<?php
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2025
//write kazua
for ($a = array(1 => "1", "1"), $i = 3; strlen($a[$i - 1]) < 1000; $i++)
$a[$i] = bcadd($a[$i - 2], $a[$i - 1]);
echo ($i - 1);
@kazua
kazua / problem007.php
Created November 6, 2013 16:05
Project Euler Problem 7(PHP)
<?php
//http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%207
//write kazua
$p = array();
$i = 2;
$f = function ($value) {
global $p;
$a = range(1, (int) sqrt($value));