Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FizzBuzz faster solution?  RSS feed

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the console should print the following:
if number is dividable with 3 = Fizz
if number is dividable with 5 = Buzz
if number is dividable with 3 and 5 = FizzBuzz
else the number from 1 to 100.



Is it possible to get a faster solution?
Because in the most cases the pc will just print out the number and not (Fizz/Buzz/FizzBuzz).
So it don't have to check if it is dividable with 3, 5 or both.

I tried to switch the statements but somehow I can't get to a faster working solution.



 
Tim Cooke
Marshal
Posts: 3853
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, your solution only counts from 1 to 99.

Secondly, how have you measured speed? What benchmarks have you measured?

Share them?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin,
If this is an assignment being done for a class, or an exercise in a book, then there is usually some discussion by the instructor, or author, on the differences between print and println; there is some expectation that the student would display understanding of that in their solution.

As for faster? The modulus operation takes a bit of resources, so you may investigate ways of doing your calculation without using it. If you calculate modulus of 3 and the modulus of 5 then do them again separately, doesn't it seem there is repetitive work being done there? (and on expensive operations too)

In coding there is always a trade off between doing something one way or another, often times it is best to know how to program clearly as the JIT compiler will do much of the optimization if you write your code cleanly using a good, or at least reasonable, algorithm.

When you run your times, you will need to do thousands or even millions of cycles to show anything meaningful, how do you suppose to do that?
Les
Kevin Olome wrote:
Is it possible to get a faster solution?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin,
Here are a few things to consider:

What happens when you execute this code?

or this?

or this?

Most reasonable minded students if they where to see this 100/7*7 would instantly conclude that since the order of precedence of division and multiplication are the same, then they can get rid of the operations done by dividing by 7 and then multiplying by 7, but that is not the case in the computer as "/" and "*" are doing their operation on integers, so we have a situation where basic integer math and precedence may yield a faster solution than doing a modulus--or may not--depending on how the modulus is implemented.

Also inequality "<" or ">" is a faster comparison than equality.

In any case it gives you something to think about.
Les
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:Is it possible to get a faster solution?
Because in the most cases the pc will just print out the number and not (Fizz/Buzz/FizzBuzz).
So it don't have to check if it is dividable with 3, 5 or both.

To know whether you need to print Fizz, Buzz, FizzBuzz or just the number you must check if the number is divisible by 3 and/or 5. If you don't know if the number is divisble by 3 and/or 5, you don't know if you need to print the number. So, obviously, there is no way to just print the number without checking if it's divisible by 3 and/or 5 - because that's exactly what you need to check to know if you should print the number or not...

Well, ofcourse there are ways to solve this more creatively. See this topic to get some inspiration. If you really want to do it without checking if numbers are divisible by 3 or 5, see this answer by Marc Weber, for example. But if you give whoever gave you this assignment an answer like that, they're probably not going to approve...
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin,
To be a programmer you are going to have to learn to do critical analysis of your code, let's take a look at what actually is happening. (I don't think your algo is nearly as efficient as you think it is):
You have the special conditions at 3, 5, and (3 and 5); as you have described. Java's conditional evaluation has a heuristic approach in that it will execute the clause until it see's that it evaluates to false or true, then skip the rest. So let's look at the first 15 cases--1 through 15:

for #1 it evals %3 (makes the clause false), then the next conditional %3 again (false), then on to the last %5, then it will print from the final else. So 3 mods and a print
for #3 it evals %3 then %5 (clause is false), then the next conditional %3 again (true), it prints from the 2nd conditional. So 3 mods and a print
for #5 it evals %3 (makes the clause false), then the next conditional %3 again (false), then on to the last %5 when it prints. So 3 mods and a print
for #15 evals %3 then %5 (clause is true) and prints from here. So 2 mods and a print. (every 15th number you save 1 mod)

So the question becomes: can you come up with an implementation that is more efficient than 294 mods and 100 prints to get the solution?

Les

Kevin Olome wrote:

Is it possible to get a faster solution?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!