Skip to content

Instantly share code, notes, and snippets.

@zerosum
Last active December 11, 2015 07:18
Show Gist options
  • Save zerosum/4565249 to your computer and use it in GitHub Desktop.
Save zerosum/4565249 to your computer and use it in GitHub Desktop.
Project Euler: Problem 1 JavaはTDDで、pythonとscalaは関数型プログラミングを目指して
package zerosum.project.euler;
public class Euler0001 {
// public int filter(int number) {
// return (number % 3 == 0 || number % 5 == 0) ? number : 0;
// }
public int execute(int number) {
int sum = 0;
for (int i = 0; i < number; i++) {
sum += (i % 3 == 0 || i % 5 == 0) ? i : 0;
}
return sum;
}
}
sum([a for a in range(1000) if a%3==0 or a%5==0])
(0 until 1000).filter(x => ( x%3==0 || x%5==0 )).sum
package zerosum.project.euler;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class Euler0001Test {
private Euler0001 sut;
@Before
public void setUp() {
sut = new Euler0001();
}
// //[x] 3の倍数ならその数を返す
// //[x] 3のときは3を返す
// @Test
// public void _3の時は3を返す() throws Exception{
// assertThat(sut.filter(3), is(3));
// }
// //[x] 6のときは6を返す
// @Test
// public void _6の時は6を返す() throws Exception{
// assertThat(sut.filter(6), is(6));
// }
// //[x] 5の倍数ならその数を返す
// //[x] 10の時は10を返す
// @Test
// public void _10の時は10を返す() throws Exception{
// assertThat(sut.filter(10), is(10));
// }
// //[x] 3の倍数でも5の倍数でもなければ0を返す
// //[x] 4の時は0を返す
// @Test
// public void _4の時は0を返す() throws Exception{
// assertThat(sut.filter(4), is(0));
// }
//[x] 10まで処理すると23が返る
@Test
public void _10まで処理すると23が返る() throws Exception{
assertThat(sut.execute(10), is(23));
}
@Test
public void execute() throws Exception{
System.out.println(sut.execute(1000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment