OpenSourcePhilosiphe

A Fast Fizzbuzz

Mar 13th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. //fizzbuzz without mod, multiplication, division, or subtraction:
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. int main()
  6. {
  7.     ///these constants are checked against the trackers
  8.     //constant for 15 bitshifts AKA binary => 1000 0000 0000 0000 (15 digit intervals with an offset of 1)
  9.     const short unsigned int  FIZZBUZZ = 0x8000;
  10.  
  11.     //this handles offsets of 15 for 5 and 10.  Multiples of 15 is handled via FIZZBUZZ
  12.     //constant for 5 bitshifts interval(bitmask) AKA binary =>0000 0100 0010 0000 (5 digit intervals with an offset of 1)
  13.     const short unsigned int  BUZZ = 0x0420;
  14.  
  15.     //this handles offsets of 15 for 3, 6, 9, and 12.  multiples of 15 is handled via FIZZBUZZ
  16.     //constant for 3 bitshifts AKA binary => 0001 0010 0100 1000 (3 digit intervals with an offset of 1)
  17.     const short unsigned int FIZZ = 0x1248;
  18.     ///end of constants
  19.  
  20.  
  21.  
  22.     ///other variables
  23.     //create your tracking variables prepped for bitshift
  24.     short unsigned int fizzBuzzAndBuzzTracker = 1;
  25.     ///end variables
  26.  
  27.  
  28.  
  29.     ///Start FizzBuzzing
  30.     //loop for 1000 itterations of fizzbuzz
  31.     for (unsigned int i=0; i <= 1000;i++)
  32.     {
  33.         if (fizzBuzzAndBuzzTracker & FIZZBUZZ)//do this if we reached a fizzbuzz(iteration is divisible by 15, (i % 15) == 0)
  34.         {
  35.             std::cout << "Fizzbuzz!";//output fizzbuzz
  36.             fizzBuzzAndBuzzTracker = 1;//reset all tracking varables
  37.         }
  38.         else if (fizzBuzzAndBuzzTracker & BUZZ)//do this if we reached a buzz(iteration is divisible by 5, (i % 5) == 0)
  39.         {
  40.             std::cout << "Buzz!";//output buzz
  41.         }
  42.         else if (fizzBuzzAndBuzzTracker & FIZZ)//do this if we reached a fizz(iteration is divisible by 3, (i % 3) == 0)
  43.         {
  44.             std::cout << "Fizz!";//output fizz
  45.         }
  46.         else//no special conditions were hit
  47.         {
  48.             //output the current iteration number
  49.             std::cout << i;
  50.         }
  51.  
  52.         std::cout << std::endl;//go to the next line
  53.  
  54.         //bitshift the tracker variables
  55.         fizzBuzzAndBuzzTracker = fizzBuzzAndBuzzTracker << 1;
  56.  
  57.     }//end fizzbuzz for()
  58.  
  59.     std::cin.get();//pause
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment