Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
And they say early optimisation is the route of all evil.. The mod operator isn't that expensive (just tried it in a loop of a trillion integers and it took 3.564 seconds, that was single threaded).
So you removed a bottleneck that never existed, meanwhile you introduced a huge amount of code repetition with code that is almost impossible to test.
finally got my day with the boss to wrap up 2011, with a fat 10% increase, feels good after all the hard work, kept me from having to take the other job offer to make ends meet
my company is getting dozens of $150K full-year contractors and making the $75K in-house developers manage them
My solution to fizz buzz: two rounds of sieve of eratosthenes. Don't all hire me at once now
Going by this description of the problem,Don't worry, we won't. Your proposed solution is somewhere between "incomplete" and "won't work".
Going by this description of the problem,
1) Start with an array with elements 1 to n.
2) Strike out multiples of 3 via SoE (i.e. say replace with -1)
3) Strike out multiples of 5 via SoE (i.e. say replace with -2); if already struck with -1, replace with say -3.
4) Scan array once and print elements, unless -1 (fizz), -2 (buzz), or -3 (fizzbuzz).
Better algorithm efficiency than any modulo solution as n grows large.
Edit: Just programmed my solution as well as a standard solution using modulo (similar to the ones in this thread), and for an input size of 100 million, here are the results:
Using modulo: 0.945289544 seconds
Using sieve: 0.088034459 seconds
Nope, got me there, the sieve needs a structure to hold the numbers afaikNow can you do it without using an array to hold all the numbers? That is, can you make it take less memory, yet still use, conceptually, the sieve algorithm? Note: I don't mean do it to multiple smaller arrays.
public void SieveFizzBuzz(int max)
{
int threes = 3;
int fives = 5;
for (int i = 1; i <= max; i++)
{
if (i != threes && i != fives)
{
printf("%d",i);
}
else
{
if (i == threes)
{
printf("Fizz");
threes += 3;
}
if (i == fives)
{
printf("Buzz");
fives += 5;
}
}
}
}
<?php
for($i=1;$i<=100;$i++){
if($i % 3 == 0 && $i % 5 == 0){
echo 'FizzBuzz';
}elseif($i % 3 == 0){
echo 'Fizz';
}elseif($i % 5 == 0){
echo 'Buzz';
}else{
echo $i;
}
}
?>
Hey, great -- now you've actually answered the question! Your algorithm still has a fatal flaw, though. And why did you decide to optimize for speed instead of memory usage?Going by this description of the problem,
Using lazy sequences, it will use significantly less memory. If you had understood how sieve of erastothenes works, I wouldn't have needed to say anything more. Why did I optimize for speed? For fun. I saw the connection and thought it was interesting.Hey, great -- now you've actually answered the question! Your algorithm still has a fatal flaw, though. And why did you decide to optimize for speed instead of memory usage?
Nope, got me there, the sieve needs a structure to hold the numbers afaik
edit: On second thought, using lazy sequences in clojure would use less memory.
Using lazy sequences, it will use significantly less memory. If you had understood how sieve of erastothenes works, I wouldn't have needed to say anything more. Why did I optimize for speed? For fun. I saw the connection and thought it was interesting.
// add -Xmx1000m to runtime options to increase heap size
public class sieve
{
public static void main(String[] args)
{
int[] arr = new int[100000000];
long endTime;
long startTime = System.nanoTime();
for (int i = 1; i < 100000000; i++)
{
if ((i % 3) == 0)
{
if ((i % 5) == 0) { } // System.out.println("fizzbuzz");
else { } // System.out.println("fizz");
}
else if ((i % 5) == 0) { } // System.out.println("buzz");
else { } // System.out.println(i);
}
endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration / 1000000000.00);
startTime = System.nanoTime();
for (int i = 0; i < 100000000; i++)
{
arr[i] = i + 1;
}
int mult = 1;
while ((3 * mult) < 100000000)
{
arr[(3 * mult) - 1] = -1;
mult += 1;
}
mult = 1;
int fiveMult = 5 * mult;
while (fiveMult < 100000000)
{
int value = arr[fiveMult - 1];
if (value == -1) arr[fiveMult - 1] = -3;
else arr[fiveMult - 1] = -2;
mult += 1;
fiveMult = 5 * mult;
}
for (int i = 1; i < 100000000; i++)
{
if (arr[i] == -1) { } // System.out.println("fizz");
else if (arr[i] == -2) {} // System.out.println("buzz");
else if (arr[i] == -3) { } // System.out.println("fizzbuzz");
else { } // System.out.println(arr[i]);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println(duration /1000000000.00);
}
}
}