Last active
December 12, 2015 07:38
-
-
Save disktnk/4738206 to your computer and use it in GitHub Desktop.
複素数偏角でFizzBuzz
元ネタ:Fizzbuzz in Complex Plane http://www.slideshare.net/KaoruMaeda/fizzbuzz-16341544
(2013/02/11 フォーマットでswitchを消せることに気付き fizzbuzz2 メソッドを追記)
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
import org.apache.commons.math3.complex.Complex; | |
public class ComplexArgFizzBuzz { | |
public String fizzbuzz(int n) { | |
Complex c = new Complex(-n % 5, n % 3); | |
int index = (int) c.getArgument(); | |
String[] fizzbuzz = { "Fizzbuzz", "Buzz", "(^^", "Fizz" }; | |
switch (index) { | |
case 2: | |
return String.valueOf(n); | |
default: | |
return fizzbuzz[index]; | |
} | |
} | |
public String fizzbuzz2(int n) { | |
String[] fizzbuzz = { "Fizzbuzz", "Buzz", "%d", "Fizz" }; | |
return String.format(fizzbuzz[(int) Complex.valueOf(-n % 5, n % 3).getArgument()], 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
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import org.junit.Test; | |
public class ComplexArgFizzBuzzTest { | |
@Test | |
public void testFizzbuzz() { | |
ComplexArgFizzBuzz target = new ComplexArgFizzBuzz(); | |
assertThat(target.fizzbuzz(1), is("1")); | |
assertThat(target.fizzbuzz(2), is("2")); | |
assertThat(target.fizzbuzz(3), is("Fizz")); | |
assertThat(target.fizzbuzz(4), is("4")); | |
assertThat(target.fizzbuzz(5), is("Buzz")); | |
assertThat(target.fizzbuzz(6), is("Fizz")); | |
assertThat(target.fizzbuzz(7), is("7")); | |
assertThat(target.fizzbuzz(8), is("8")); | |
assertThat(target.fizzbuzz(9), is("Fizz")); | |
assertThat(target.fizzbuzz(10), is("Buzz")); | |
assertThat(target.fizzbuzz(11), is("11")); | |
assertThat(target.fizzbuzz(12), is("Fizz")); | |
assertThat(target.fizzbuzz(13), is("13")); | |
assertThat(target.fizzbuzz(14), is("14")); | |
assertThat(target.fizzbuzz(15), is("Fizzbuzz")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍