Skip to content

Instantly share code, notes, and snippets.

@disktnk
Last active December 12, 2015 07:38
Show Gist options
  • Save disktnk/4738206 to your computer and use it in GitHub Desktop.
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 メソッドを追記)
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);
}
}
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"));
}
}
@mad-p
Copy link

mad-p commented Nov 5, 2013

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment