FizzBuzz implementation in Java without modulus operator
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I was trying to implement FizzBuzz without modulus operator.
Areas of concern: inner for loop maybe not needed? seq
array maybe not needed?
class FizzBuzzWithoutModulus
public static void main(String args)
int fizzbuzz = new int[101];
int buzz = new int[101];
int fizz = new int[101];
int seq = new int[101];
for(int i=1; i<=100; i++)
seq[i] = i;
for(int i=1; i<=100; i++)
for(int j=1; j<seq.length; j++)
if((i/15.0) == seq[j])
fizzbuzz[i] = i;
else if((i/5.0) == seq[j])
buzz[i] = i;
else if((i/3.0) == seq[j])
fizz[i] = i;
for(int i=0; i<=100; i++)
if(fizzbuzz[i]!=0)
System.out.println(fizzbuzz[i] + " fizzbuzz");
else if(buzz[i]!=0)
System.out.println(buzz[i] + " buzz");
else if(fizz[i]!=0)
System.out.println(fizz[i] + " fizz");
java fizzbuzz
add a comment |Â
up vote
2
down vote
favorite
I was trying to implement FizzBuzz without modulus operator.
Areas of concern: inner for loop maybe not needed? seq
array maybe not needed?
class FizzBuzzWithoutModulus
public static void main(String args)
int fizzbuzz = new int[101];
int buzz = new int[101];
int fizz = new int[101];
int seq = new int[101];
for(int i=1; i<=100; i++)
seq[i] = i;
for(int i=1; i<=100; i++)
for(int j=1; j<seq.length; j++)
if((i/15.0) == seq[j])
fizzbuzz[i] = i;
else if((i/5.0) == seq[j])
buzz[i] = i;
else if((i/3.0) == seq[j])
fizz[i] = i;
for(int i=0; i<=100; i++)
if(fizzbuzz[i]!=0)
System.out.println(fizzbuzz[i] + " fizzbuzz");
else if(buzz[i]!=0)
System.out.println(buzz[i] + " buzz");
else if(fizz[i]!=0)
System.out.println(fizz[i] + " fizz");
java fizzbuzz
Why are you dividing by a float instead of an integer?
â Mast
May 12 at 19:51
@Mast float literal is needed to avoid Java automatically truncate the int. Otherwise 47/15 for example will evaluate to 3, erroneously flagging 47 as fizzbuzz.
â BridgeWall
May 12 at 20:26
Accessing an element of an array is $O(1)$ and probably pretty fast, so the calls toseq[j]
should not be a bottleneck. However, the initial filling up ofseq
takes time, which you could save if you replaced the arrayseq
with a methodint seq(int n) return n;
.
â Stingy
May 12 at 21:36
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I was trying to implement FizzBuzz without modulus operator.
Areas of concern: inner for loop maybe not needed? seq
array maybe not needed?
class FizzBuzzWithoutModulus
public static void main(String args)
int fizzbuzz = new int[101];
int buzz = new int[101];
int fizz = new int[101];
int seq = new int[101];
for(int i=1; i<=100; i++)
seq[i] = i;
for(int i=1; i<=100; i++)
for(int j=1; j<seq.length; j++)
if((i/15.0) == seq[j])
fizzbuzz[i] = i;
else if((i/5.0) == seq[j])
buzz[i] = i;
else if((i/3.0) == seq[j])
fizz[i] = i;
for(int i=0; i<=100; i++)
if(fizzbuzz[i]!=0)
System.out.println(fizzbuzz[i] + " fizzbuzz");
else if(buzz[i]!=0)
System.out.println(buzz[i] + " buzz");
else if(fizz[i]!=0)
System.out.println(fizz[i] + " fizz");
java fizzbuzz
I was trying to implement FizzBuzz without modulus operator.
Areas of concern: inner for loop maybe not needed? seq
array maybe not needed?
class FizzBuzzWithoutModulus
public static void main(String args)
int fizzbuzz = new int[101];
int buzz = new int[101];
int fizz = new int[101];
int seq = new int[101];
for(int i=1; i<=100; i++)
seq[i] = i;
for(int i=1; i<=100; i++)
for(int j=1; j<seq.length; j++)
if((i/15.0) == seq[j])
fizzbuzz[i] = i;
else if((i/5.0) == seq[j])
buzz[i] = i;
else if((i/3.0) == seq[j])
fizz[i] = i;
for(int i=0; i<=100; i++)
if(fizzbuzz[i]!=0)
System.out.println(fizzbuzz[i] + " fizzbuzz");
else if(buzz[i]!=0)
System.out.println(buzz[i] + " buzz");
else if(fizz[i]!=0)
System.out.println(fizz[i] + " fizz");
java fizzbuzz
edited May 12 at 19:50
Mast
7,32563484
7,32563484
asked May 12 at 19:39
BridgeWall
112
112
Why are you dividing by a float instead of an integer?
â Mast
May 12 at 19:51
@Mast float literal is needed to avoid Java automatically truncate the int. Otherwise 47/15 for example will evaluate to 3, erroneously flagging 47 as fizzbuzz.
â BridgeWall
May 12 at 20:26
Accessing an element of an array is $O(1)$ and probably pretty fast, so the calls toseq[j]
should not be a bottleneck. However, the initial filling up ofseq
takes time, which you could save if you replaced the arrayseq
with a methodint seq(int n) return n;
.
â Stingy
May 12 at 21:36
add a comment |Â
Why are you dividing by a float instead of an integer?
â Mast
May 12 at 19:51
@Mast float literal is needed to avoid Java automatically truncate the int. Otherwise 47/15 for example will evaluate to 3, erroneously flagging 47 as fizzbuzz.
â BridgeWall
May 12 at 20:26
Accessing an element of an array is $O(1)$ and probably pretty fast, so the calls toseq[j]
should not be a bottleneck. However, the initial filling up ofseq
takes time, which you could save if you replaced the arrayseq
with a methodint seq(int n) return n;
.
â Stingy
May 12 at 21:36
Why are you dividing by a float instead of an integer?
â Mast
May 12 at 19:51
Why are you dividing by a float instead of an integer?
â Mast
May 12 at 19:51
@Mast float literal is needed to avoid Java automatically truncate the int. Otherwise 47/15 for example will evaluate to 3, erroneously flagging 47 as fizzbuzz.
â BridgeWall
May 12 at 20:26
@Mast float literal is needed to avoid Java automatically truncate the int. Otherwise 47/15 for example will evaluate to 3, erroneously flagging 47 as fizzbuzz.
â BridgeWall
May 12 at 20:26
Accessing an element of an array is $O(1)$ and probably pretty fast, so the calls to
seq[j]
should not be a bottleneck. However, the initial filling up of seq
takes time, which you could save if you replaced the array seq
with a method int seq(int n) return n;
.â Stingy
May 12 at 21:36
Accessing an element of an array is $O(1)$ and probably pretty fast, so the calls to
seq[j]
should not be a bottleneck. However, the initial filling up of seq
takes time, which you could save if you replaced the array seq
with a method int seq(int n) return n;
.â Stingy
May 12 at 21:36
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
I think this implementation is really too confusion for the problem at hand, although I think it is quite an interesting solution.
As you said it yourself, integer division loses the fractional part (rounds down towards 0). You can actually use this for your solution! Because if a number is not evenly divisible, multiplying with the division result will not give you back the original number, it will be smaller!
So a way to do this would be:
public static String fizzBuzz(final int number)
if (number / 15 * 15 == number)
return "FizzBuzz";
else if (number / 5 * 5 == number)
return "Buzz";
else if (number / 3 * 3 == number)
return "Fizz";
return Integer.toString(i);
Note that a number divisible by 3 and 5 is also divisible by 15.
add a comment |Â
up vote
1
down vote
I think there is an error because in fizzbuzz you usually just print out the raw number if it's not a multiple of 3 or 5.
I don't like hard coded "magic numbers". It's better to define a constant final int n = 100
and use that everywhere. If you change it later, you won't have to worry that maybe you missed an instance. An alternative would be to write this as a function and have one input parameter n
.
Instead of the inner loop, using a binary search would be a lot more efficient.
I would probably rename seq
to integers
. Also, seq
does not have to go to 100, but could stop at roughly 100 / 3.
Whenever you check if the divided number is an integer, eg. (i/15.0) == seq[j]
, I would probably write that as a function checkIfInteger(i / 15.0)
. That function could have the same implementation that you have with seq
, or any other implementation. This is the principle of "separation of concerns": you basically break up your code in smaller parts so it's easier to read and maintain.
This type of problem is a natural fit with modern Streams, which are a more readable alternative to for
-loops in some cases. For example, for the standard fizzbuzz algo (copied from @Ronald Raab's answer here):
IntStream.rangeClosed(0, 100).mapToObj(
i -> i % 3 == 0 ?
(i % 5 == 0 ? "FizzBuzz" : "Fizz") :
(i % 5 == 0 ? "Buzz" : i))
.forEach(System.out::println);
add a comment |Â
up vote
0
down vote
One option you have is to use your own mod
method. If you subtract the answer after the div and multiply you get a modulus the same as the modulus operator, which can go in a method.
By using floating indexes for the whole combined string, you can simplify by only checking for each divisor and returning however much of the string is needed. Something like this:
public static int mod(int num, int divisor)
return num - ((num / divisor) * divisor);
public static String fizzBuzz(int num, int divisor1, int divisor2)
final String answer = "FizzBuzz";
final int firstStart = 0;
final int firstEnd = 4;
final int secondStart = 4;
final int secondEnd = 8;
int start = firstStart;
int end = start;
if (mod(num, divisor1) == 0)
end = firstEnd;
if (mod(num, divisor2) == 0)
if (end == 0)
start = secondStart;
end = secondEnd;
if (end > 0)
return answer.substring(start, end);
return Integer.toString(num);
The ends
are set to 1 past the actual end to simplify calling substring
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
I think this implementation is really too confusion for the problem at hand, although I think it is quite an interesting solution.
As you said it yourself, integer division loses the fractional part (rounds down towards 0). You can actually use this for your solution! Because if a number is not evenly divisible, multiplying with the division result will not give you back the original number, it will be smaller!
So a way to do this would be:
public static String fizzBuzz(final int number)
if (number / 15 * 15 == number)
return "FizzBuzz";
else if (number / 5 * 5 == number)
return "Buzz";
else if (number / 3 * 3 == number)
return "Fizz";
return Integer.toString(i);
Note that a number divisible by 3 and 5 is also divisible by 15.
add a comment |Â
up vote
3
down vote
I think this implementation is really too confusion for the problem at hand, although I think it is quite an interesting solution.
As you said it yourself, integer division loses the fractional part (rounds down towards 0). You can actually use this for your solution! Because if a number is not evenly divisible, multiplying with the division result will not give you back the original number, it will be smaller!
So a way to do this would be:
public static String fizzBuzz(final int number)
if (number / 15 * 15 == number)
return "FizzBuzz";
else if (number / 5 * 5 == number)
return "Buzz";
else if (number / 3 * 3 == number)
return "Fizz";
return Integer.toString(i);
Note that a number divisible by 3 and 5 is also divisible by 15.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
I think this implementation is really too confusion for the problem at hand, although I think it is quite an interesting solution.
As you said it yourself, integer division loses the fractional part (rounds down towards 0). You can actually use this for your solution! Because if a number is not evenly divisible, multiplying with the division result will not give you back the original number, it will be smaller!
So a way to do this would be:
public static String fizzBuzz(final int number)
if (number / 15 * 15 == number)
return "FizzBuzz";
else if (number / 5 * 5 == number)
return "Buzz";
else if (number / 3 * 3 == number)
return "Fizz";
return Integer.toString(i);
Note that a number divisible by 3 and 5 is also divisible by 15.
I think this implementation is really too confusion for the problem at hand, although I think it is quite an interesting solution.
As you said it yourself, integer division loses the fractional part (rounds down towards 0). You can actually use this for your solution! Because if a number is not evenly divisible, multiplying with the division result will not give you back the original number, it will be smaller!
So a way to do this would be:
public static String fizzBuzz(final int number)
if (number / 15 * 15 == number)
return "FizzBuzz";
else if (number / 5 * 5 == number)
return "Buzz";
else if (number / 3 * 3 == number)
return "Fizz";
return Integer.toString(i);
Note that a number divisible by 3 and 5 is also divisible by 15.
answered May 12 at 20:48
Koekje
1,017211
1,017211
add a comment |Â
add a comment |Â
up vote
1
down vote
I think there is an error because in fizzbuzz you usually just print out the raw number if it's not a multiple of 3 or 5.
I don't like hard coded "magic numbers". It's better to define a constant final int n = 100
and use that everywhere. If you change it later, you won't have to worry that maybe you missed an instance. An alternative would be to write this as a function and have one input parameter n
.
Instead of the inner loop, using a binary search would be a lot more efficient.
I would probably rename seq
to integers
. Also, seq
does not have to go to 100, but could stop at roughly 100 / 3.
Whenever you check if the divided number is an integer, eg. (i/15.0) == seq[j]
, I would probably write that as a function checkIfInteger(i / 15.0)
. That function could have the same implementation that you have with seq
, or any other implementation. This is the principle of "separation of concerns": you basically break up your code in smaller parts so it's easier to read and maintain.
This type of problem is a natural fit with modern Streams, which are a more readable alternative to for
-loops in some cases. For example, for the standard fizzbuzz algo (copied from @Ronald Raab's answer here):
IntStream.rangeClosed(0, 100).mapToObj(
i -> i % 3 == 0 ?
(i % 5 == 0 ? "FizzBuzz" : "Fizz") :
(i % 5 == 0 ? "Buzz" : i))
.forEach(System.out::println);
add a comment |Â
up vote
1
down vote
I think there is an error because in fizzbuzz you usually just print out the raw number if it's not a multiple of 3 or 5.
I don't like hard coded "magic numbers". It's better to define a constant final int n = 100
and use that everywhere. If you change it later, you won't have to worry that maybe you missed an instance. An alternative would be to write this as a function and have one input parameter n
.
Instead of the inner loop, using a binary search would be a lot more efficient.
I would probably rename seq
to integers
. Also, seq
does not have to go to 100, but could stop at roughly 100 / 3.
Whenever you check if the divided number is an integer, eg. (i/15.0) == seq[j]
, I would probably write that as a function checkIfInteger(i / 15.0)
. That function could have the same implementation that you have with seq
, or any other implementation. This is the principle of "separation of concerns": you basically break up your code in smaller parts so it's easier to read and maintain.
This type of problem is a natural fit with modern Streams, which are a more readable alternative to for
-loops in some cases. For example, for the standard fizzbuzz algo (copied from @Ronald Raab's answer here):
IntStream.rangeClosed(0, 100).mapToObj(
i -> i % 3 == 0 ?
(i % 5 == 0 ? "FizzBuzz" : "Fizz") :
(i % 5 == 0 ? "Buzz" : i))
.forEach(System.out::println);
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I think there is an error because in fizzbuzz you usually just print out the raw number if it's not a multiple of 3 or 5.
I don't like hard coded "magic numbers". It's better to define a constant final int n = 100
and use that everywhere. If you change it later, you won't have to worry that maybe you missed an instance. An alternative would be to write this as a function and have one input parameter n
.
Instead of the inner loop, using a binary search would be a lot more efficient.
I would probably rename seq
to integers
. Also, seq
does not have to go to 100, but could stop at roughly 100 / 3.
Whenever you check if the divided number is an integer, eg. (i/15.0) == seq[j]
, I would probably write that as a function checkIfInteger(i / 15.0)
. That function could have the same implementation that you have with seq
, or any other implementation. This is the principle of "separation of concerns": you basically break up your code in smaller parts so it's easier to read and maintain.
This type of problem is a natural fit with modern Streams, which are a more readable alternative to for
-loops in some cases. For example, for the standard fizzbuzz algo (copied from @Ronald Raab's answer here):
IntStream.rangeClosed(0, 100).mapToObj(
i -> i % 3 == 0 ?
(i % 5 == 0 ? "FizzBuzz" : "Fizz") :
(i % 5 == 0 ? "Buzz" : i))
.forEach(System.out::println);
I think there is an error because in fizzbuzz you usually just print out the raw number if it's not a multiple of 3 or 5.
I don't like hard coded "magic numbers". It's better to define a constant final int n = 100
and use that everywhere. If you change it later, you won't have to worry that maybe you missed an instance. An alternative would be to write this as a function and have one input parameter n
.
Instead of the inner loop, using a binary search would be a lot more efficient.
I would probably rename seq
to integers
. Also, seq
does not have to go to 100, but could stop at roughly 100 / 3.
Whenever you check if the divided number is an integer, eg. (i/15.0) == seq[j]
, I would probably write that as a function checkIfInteger(i / 15.0)
. That function could have the same implementation that you have with seq
, or any other implementation. This is the principle of "separation of concerns": you basically break up your code in smaller parts so it's easier to read and maintain.
This type of problem is a natural fit with modern Streams, which are a more readable alternative to for
-loops in some cases. For example, for the standard fizzbuzz algo (copied from @Ronald Raab's answer here):
IntStream.rangeClosed(0, 100).mapToObj(
i -> i % 3 == 0 ?
(i % 5 == 0 ? "FizzBuzz" : "Fizz") :
(i % 5 == 0 ? "Buzz" : i))
.forEach(System.out::println);
edited May 12 at 22:45
answered May 12 at 22:36
toto2
5,1571019
5,1571019
add a comment |Â
add a comment |Â
up vote
0
down vote
One option you have is to use your own mod
method. If you subtract the answer after the div and multiply you get a modulus the same as the modulus operator, which can go in a method.
By using floating indexes for the whole combined string, you can simplify by only checking for each divisor and returning however much of the string is needed. Something like this:
public static int mod(int num, int divisor)
return num - ((num / divisor) * divisor);
public static String fizzBuzz(int num, int divisor1, int divisor2)
final String answer = "FizzBuzz";
final int firstStart = 0;
final int firstEnd = 4;
final int secondStart = 4;
final int secondEnd = 8;
int start = firstStart;
int end = start;
if (mod(num, divisor1) == 0)
end = firstEnd;
if (mod(num, divisor2) == 0)
if (end == 0)
start = secondStart;
end = secondEnd;
if (end > 0)
return answer.substring(start, end);
return Integer.toString(num);
The ends
are set to 1 past the actual end to simplify calling substring
add a comment |Â
up vote
0
down vote
One option you have is to use your own mod
method. If you subtract the answer after the div and multiply you get a modulus the same as the modulus operator, which can go in a method.
By using floating indexes for the whole combined string, you can simplify by only checking for each divisor and returning however much of the string is needed. Something like this:
public static int mod(int num, int divisor)
return num - ((num / divisor) * divisor);
public static String fizzBuzz(int num, int divisor1, int divisor2)
final String answer = "FizzBuzz";
final int firstStart = 0;
final int firstEnd = 4;
final int secondStart = 4;
final int secondEnd = 8;
int start = firstStart;
int end = start;
if (mod(num, divisor1) == 0)
end = firstEnd;
if (mod(num, divisor2) == 0)
if (end == 0)
start = secondStart;
end = secondEnd;
if (end > 0)
return answer.substring(start, end);
return Integer.toString(num);
The ends
are set to 1 past the actual end to simplify calling substring
add a comment |Â
up vote
0
down vote
up vote
0
down vote
One option you have is to use your own mod
method. If you subtract the answer after the div and multiply you get a modulus the same as the modulus operator, which can go in a method.
By using floating indexes for the whole combined string, you can simplify by only checking for each divisor and returning however much of the string is needed. Something like this:
public static int mod(int num, int divisor)
return num - ((num / divisor) * divisor);
public static String fizzBuzz(int num, int divisor1, int divisor2)
final String answer = "FizzBuzz";
final int firstStart = 0;
final int firstEnd = 4;
final int secondStart = 4;
final int secondEnd = 8;
int start = firstStart;
int end = start;
if (mod(num, divisor1) == 0)
end = firstEnd;
if (mod(num, divisor2) == 0)
if (end == 0)
start = secondStart;
end = secondEnd;
if (end > 0)
return answer.substring(start, end);
return Integer.toString(num);
The ends
are set to 1 past the actual end to simplify calling substring
One option you have is to use your own mod
method. If you subtract the answer after the div and multiply you get a modulus the same as the modulus operator, which can go in a method.
By using floating indexes for the whole combined string, you can simplify by only checking for each divisor and returning however much of the string is needed. Something like this:
public static int mod(int num, int divisor)
return num - ((num / divisor) * divisor);
public static String fizzBuzz(int num, int divisor1, int divisor2)
final String answer = "FizzBuzz";
final int firstStart = 0;
final int firstEnd = 4;
final int secondStart = 4;
final int secondEnd = 8;
int start = firstStart;
int end = start;
if (mod(num, divisor1) == 0)
end = firstEnd;
if (mod(num, divisor2) == 0)
if (end == 0)
start = secondStart;
end = secondEnd;
if (end > 0)
return answer.substring(start, end);
return Integer.toString(num);
The ends
are set to 1 past the actual end to simplify calling substring
answered May 13 at 3:31
tinstaafl
5,482625
5,482625
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194273%2ffizzbuzz-implementation-in-java-without-modulus-operator%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Why are you dividing by a float instead of an integer?
â Mast
May 12 at 19:51
@Mast float literal is needed to avoid Java automatically truncate the int. Otherwise 47/15 for example will evaluate to 3, erroneously flagging 47 as fizzbuzz.
â BridgeWall
May 12 at 20:26
Accessing an element of an array is $O(1)$ and probably pretty fast, so the calls to
seq[j]
should not be a bottleneck. However, the initial filling up ofseq
takes time, which you could save if you replaced the arrayseq
with a methodint seq(int n) return n;
.â Stingy
May 12 at 21:36