ãªã£ã¡ããï¼ä»¥ä¸ãªï¼ããªãããã¼ãã¿ã¤ãã«ãç¡çãããªãï¼ã
ããããï¼ä»¥ä¸ãï¼ãã¾ãæµè¡ãã ãããã
ãã£ã¡ããï¼ä»¥ä¸ãï¼ãæµè¡ãã£ã¦è¨ã£ã¦ãFizzBuzzを30バイトで - Togetterã1/24ã ãã1é±éçµã£ã¦ããã©ãã©ã¡ããã¨è¨ãã°ãªã¯ã³ã³ï¼ã
ãªãã¾ãã¾ãã
ããã¨ãããããã©ã®ãããç¡çã£ã½ããè¦ã¦ã¿ãããï¼ã
æ®éã®FizzBuzz
ãããããã¾ãæ®éã®FizzBuzzãã¿ã¦ã¿ã¾ãããã
ãªãFizzBuzz Javaã§ã°ã°ã£ã¦â¦ããããªï¼ Fizz Buzz - Semicolonless Javaã
public class FizzBuzz { public static void main(String[] args) { for (int i : new int[] { 0 }) { while (++i <= 100) { if (i % 15 == 0) { if (null == System.out.printf("FizzBuzz\n")) {} } else if (i % 3 == 0) { if (null == System.out.printf("Fizz\n")) {} } else if (i % 5 == 0) { if (null == System.out.printf("Buzz\n")) {} } else { if (null == System.out.printf("%d\n", i)) {} } } } } }
ããéãï¼ããSemicolonlessJavaãããããï¼ã
ãªããã¼ã1ãã¼ã¸ç®ã«åºã¦ãããï¼ã
ããããã¯ä¸ãæ«ãããã®ã¸ãããããããããããhttp://homepage2.nifty.com/igat/igapyon/diary/2007/ig070702.htmlã
/** * FizzBuzz: ã¤ã®ãªã¹ã®å¦æ ¡ã®åä¾ãã¡ã®éã³ * * 1ãã100ã¾ã§ã®æ°ãããªã³ãããã<br> * ã»3ã®åæ°ã®ã¨ãã¯æ°ã®ä»£ããã«ï½¢Fizzï½£ã¨ããªã³ãããã<br> * ã»5ã®åæ°ã®ã¨ãã¯æ°ã®ä»£ããã«ï½¢Buzzï½£ã¨ããªã³ãããã<br> * ã»3ã¨5両æ¹ã®åæ°ã®å ´åã«ã¯æ°ã®ä»£ããã«ï½¢FizzBuzzï½£ã¨ããªã³ãããã */ public class FizzBuzzNormal04 { /** * ã¨ã³ããªãã¤ã³ãã * * @param args * ã³ãã³ãã©ã¤ã³å¼æ°ããã®ããã°ã©ã å ã§ã¯å©ç¨ãããªãã */ public static void main(final String[] args) { // TODO:ä¸é¨å ±éåãå®æ½ãã¾ããã // TODO:ãã¸ãã¯ãã³ãã¼ã¯è¿½æ¾ããã¦ãã¾ããã for (int number = 1; number <= 100; number++) { // 3ã®åæ°ã§ãããã©ããã final boolean isMultipleOf3 = (number % 3 == 0); // 5ã®åæ°ã§ãããã©ããã final boolean isMultipleOf5 = (number % 5 == 0); final StringBuffer bufMsg = new StringBuffer(); if (isMultipleOf3 && isMultipleOf5) { // 3ã¨5両æ¹ã®åæ°ã®å ´åã«ã¯ï½¢FizzBuzzï½£ã¨ã»ããããã bufMsg.append("FizzBuzz"); } else if (isMultipleOf3) { // 3ã®åæ°ã®ã¨ãã¯æ°ã®ä»£ããã«ï½¢Fizzï½£ã¨ã»ããããã bufMsg.append("Fizz"); } else if (isMultipleOf5) { // 5ã®åæ°ã®ã¨ãã¯ï½¢Buzzï½£ã¨ã»ããããã bufMsg.append("Buzz"); } else { // æ°ãã»ããããã bufMsg.append(number); } // çæãããæååãããªã³ãããã System.out.println(bufMsg.toString()); } } }
å¿ è¦ãªæå
ãªãpublic class F{}ã£ã¦ã®ã¯å¤ããªãããï¼ã
ããæ¨æºåºåã«åºããªãSystem.out.print()ãå¤ããªãããã
ãªããã¨public static void main(String[] a){}ããªã
ããæååãªãã©ã«ã®"Fizz"ã¨"Buzz"ãå¿
è¦ã ãªã
ãã16+18+37+6+6=87ãªãã ãã©ã
ãªãhttp://www.shinh.org/p.rb?FizzBuzz#Javaã ã¨Javaã¯97æåãæçè¨é²ã¿ããã
ããããã¯ã·ã§ã¼ãã³ã¼ãã£ã³ã°ï¼ãããã¯ã³ã¼ãã´ã«ãï¼ã¨å¼ã°ããã¸ã£ã³ã«ãªãã ãèå³ãããã°http://www.amazon.co.jp/dp/4839925232ãèªãã¨ããã
ãããããããã¢ãã¯ãªæ¬ããã£ããã®ãã
ãªããä»äºã¨ãããã¾ã£ãã使ããªããã¯ããã¯æºè¼ãã
ããä¾ãã°mainã¡ã½ããã¯æ¸ããªãã®ãJavaã®ã·ã§ã¼ãã³ã¼ãã£ã³ã°ã®åºæ¬ã ã
ãªããªã«ãªã«â¦ãstaticåæåãããã¯ã«ã³ã¼ããæ¸ãã£ã¦ï¼ã
public class F{ static{ System.out.println("HelloWorld!"); } }
ããã³ã³ãã¤ã«ã¯ã§ãããã©ãå®è¡ã¯ã§ããªãããããªãï¼ã
ãããããã«ä¾å¤ã¯åºãã
HelloWorld! Exception in thread "main" java.lang.NoSuchMethodError: main
ãªãä¾å¤ã®åã«HelloWorld!ã£ã¦ã§ã¦ããã
ãããããã¯ã©ã¹ããã¼ãæã®å®è¡é çªãæãè¿ãããç¶æ¿é層ã®ãã¨ã¯ä»åã¯ããã¦ããã¨ãã¦ãstaticåæåãããã¯ãstaticãã£ã¼ã«ãã®åæåãã¾ãè¡ããããã
ãããã®æç¹ã§staticåæåãããã¯ãå®è¡ããã¦ãããããmainã¡ã½ããããªãã£ã¦è¨ãããããã*1ã
ãªã37æåã®mainã¡ã½ããã®å®£è¨ã8æåã«ç縮ããããã
ããã¤ãã§ã«class宣è¨ã®publicãå¿
è¦ãªãã
ã·ã§ã¼ãã³ã¼ãã£ã³ã°ã«ææ¦
ããããã§ã³ã¼ããæ¸ãã¦ã¿ãããã
class F{ static{ for(int i=1;i<101;i++){ System.out.println((i%15==0)?"FizzBuzz":(i%3==0)?"Fizz":(i%5==0)?"Buzz":i); } } }
ããã¤ã³ãã³ãã¨æ¹è¡ãé¤ãã°116æåãã
ãªã"FizzBuzz"ã£ã¦ã¨ããããªãã¨ããªããªãã®ãããã
ãããããå°å³ã«é£ãããã¤ã³ãã ãªãä¾ãã°ã
System.out.println((i%3==0)?"Fizz":""+((i%5==0)?"Buzz":i));
ããã£ã¦ãªæãã«ãã¦ååã¨å¾åãæååé£çµã«ããã¨ã
1 2 Fizz3 4 Buzz Fizz6 7 8 Fizz9 Buzz 11 Fizz12 13 14 FizzBuzz
ãªãããããFizzã®å¾ãã«ä½è¨ãªã®ãã¤ãã¡ããã®ãã
ããiã®æã«ã¾ã3é
æ¼ç®åããããï¼ã
System.out.println((i%3==0)?"Fizz":""+((i%5==0)?"Buzz":(i%3==0)?"":i));
ãªããã®é¨åã§71æåãæåã®ã75æåã ãã4æåããæ¸ããªããã
ãã3é
æ¼ç®åã®æ¡ä»¶é¨åã==0ãããªãã¦>0ã«ãã¦ãthenã¨elseãéã«ãã¦ã¿ããã
System.out.println((i%3==0)?"Fizz":""+((i%5>0)?(i%3>0)?i:"":"Buzz"));
ããååé¨åã>0ã«ããã¨ãã¾ãåããªããªããæ¼ç®åã®åªå
é ä½ããããããã¨ããã ãªã
ãªãããã§2æå縮ã¾ã£ããï¼ããã§110æåï¼ã
ããã¡ãã£ã¨ã¾ã£ã¦ãJavaã®æçè¨é²ã®97æåã¾ã§ãã¨13æåãããã®ãï¼ã
ãªããããã30æåã¨ãã©ããããã¦ãç¡çãããªãï¼ã
ãã30æåã¯ç¡çã ãã©ã30byteãªãæã¯ãããã
ãªããããããã³ãããã¼ï¼ã
1æå != 8bit
ããASCIIã³ã¼ãã£ã¦7bitãªã®ã¯ç¥ã£ã¦ãï¼ã
ãªããã£ã
ãããã®æã1byteã7bitã®ã³ã³ãã¥ã¼ã¿ããã£ãã¨ã(参考)ã
ããFizzBuzzã110æåã§æ¸ãããªãã1æå2bitã®æåã³ã¼ãã§è¨è¿°ããã°220bitã§è¨è¿°ã§ãããbyteã«ç´ãã¨28byteã«ãªãã
ãªãç¡è¶è¨ããªãã
ããã ãããã1æå2bitããæåã4種é¡ããæ±ããªããããªãã
ããã¾ãä¾ãã°ã®è©±ã ããä½ããã®ãããåãå
ã«FizzBuzzã®ããã°ã©ã ã«å±éããæåã³ã¼ãããã£ãã¨ãããã©ãã ãï¼ã
æåã³ã¼ããä½ã
ãªããããªãã¨è¨ã£ã¦ãæåã³ã¼ããªãã¦ã©ããã£ã¦ä½ãã®ãï¼ã
ãããµãµã£ãJavaããã¾ãçãè¦ãªãã»ããããããããããã¨ãåºæ¥ãããã«ãªã£ã¦ãããã ããåèãµã¤ãã¯ãããhttp://www.utilz.jp/wiki/Charsetã
ããä»åã¯ããã®TestDecoderã«ç´°å·¥ãããã
import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CoderResult; public class TestDecoder extends CharsetDecoder{ public TestDecoder(Charset cs){ super(cs, 0.5f, 1.0f); } public CoderResult decodeLoop( ByteBuffer in, CharBuffer out){ out.put("class F{static{for(int i=1;i<101;i++){" + "System.out.println((i%3==0)?\"Fizz\":\"\"+((i%5>0)?(i%3>0)?i:\"\":\"Buzz\"));}}}"); return CoderResult.UNDERFLOW; } }
ãªãã¡ãâ¦ã
ããããã¾ãã ãâ¦ãæ¥ãå¤èããªãããâ¦ã
ãã0byte FizzBuzzã£ã¦ããã ã
å®è¡ç°å¢
ãããã¤ã¯ãããjavacã«æåã³ã¼ãããªããªãèªèããããªãã¦ãã
ãªãé§ç®ãããªãã
ãããããããªãããtoolkit使ã£ã¦Javaä¸ã§javacãå¼ã³åºãããã ã
ãããããã«ããã´ã«ãå ´*2ã§ã¯éç¨ããªããããã
ãããå¾ãããããããã§ã
*1:ã³ã¼ãã´ã«ãã®ãµã¼ãã«ãã£ã¦ã¯ä¾å¤ãåºã¦ãæåã¨ãããã±ã¼ã¹ããããããã§ã¯ãªãå ´åã«ã¯System.exit(0);ã¨ãããªã©å¥ã®æãèããªãã¦ã¯ãªããªããã¤ã¡ã¼ã¸ã¨ãã¦ã¯ãµã¼ãä¸ã§åãJUnitã®ãããªèªåãã¹ããã¯ãªã¢ãããããã°ããããã¾ã«ãã¹ãã«ç©´ããã£ã¦æé©åããã¦ãã¾ãã±ã¼ã¹ããã
*2:ã³ã¼ãã´ã«ããéå¬ãã¦ãããµã¼ãã®ãã¨