[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Hey lads, wanna see a fizzbuzz implementation that doesn't

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 5
Thread images: 1

File: sadpanda.jpg (9KB, 260x260px) Image search: [Google]
sadpanda.jpg
9KB, 260x260px
Hey lads, wanna see a fizzbuzz implementation that doesn't use booleans?
String[][] arr = {{"fizzbuzz\n","fizz\n"},{"buzz\n","%d\n"}};
for (int i = 1; i <= 100; ++i) System.out.printf(arr[i%3 - (i%3 >> 1)][(i%5 - (i%5 >> 1)) - ((i%5 - (i%5 >> 1)) >> 1)], i);

pic unrelated
>>
Except for the for loop, of course.
>>
>>62466350
Nice, collapsing loops into optimized tesseracts is the best drug anon.
>>
I am a brainlet, can someone explain what is happening here? how does this work?
>>
>>62467872
i%3 can result in 0, 1, or 2;
0 - (0 >> 1) = 0; 1 - (1 >> 1) = 1; 2 - (2 >> 1) = 1.

i%5 can result in 0, 1, 2, 3, or 4;
3 - (3 >> 1) = 2; 4 - (4 >> 1) = 2; reapplying the operation for the resultant 2s yield 1s (and reapplying for 0 or 1 yields themselves).

so nesting the [x%y - (x%y >> 1)] n = floor(log_2(y)) times will yield 1 for x%y != 0 and 0 for x%y = 0

so now we can make a 2 dimensional array 2x2with one of the dimensions handling divisibility by 3 and the other for divisibility by 5. (i.e. [0][0] is the state of being divisible by both [0][1] being divisible by 3 only etc.)

and the printf is to handle when they're not divisible by either.
Thread posts: 5
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.