Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Golfed 8 bytes
Source Link
DLosc
  • 40.7k
  • 6
  • 87
  • 142

QBasicQB64, 102(削除) 102 (削除ここまで) 94 bytes

Tested on QB64 .

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THENIF""<o$THEN?MID$(STR$(o$ELSE WRITE i),2)ELSE?o$
NEXT

Doesn't work on actual QBasic; see below for why.

This program has one problem: QBasic/QB64 outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation:

Ungolfed code and explanation

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" =< o$ THEN
 PRINT MID$(STR$(i), 2)o$
 ELSE
 PRINTWRITE o$i
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and printoutput o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

The other difficulttricky part is printing numbers according to the spec. QBasic printsQBasic's PRINT command outputs positive numbers with leading spaces, which is occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$WRITE command, however, does not add a leading space--perfect for our purposes here.

1Strings1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". FortunatelyIn actual QBasic, even though 0 isn't a legal index and gives Illegal function call; but in QB64, MID$("Fizz",0) happily returns the whole string instead of complaining.

QBasic, 102 bytes

Tested on QB64 .

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation:

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" = o$ THEN
 PRINT MID$(STR$(i), 2)
 ELSE
 PRINT o$
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and print o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

The other difficult part is printing numbers according to the spec. QBasic prints positive numbers with leading spaces, which is occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$.

1Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". Fortunately, even though 0 isn't a legal index, MID$("Fizz",0) happily returns the whole string instead of complaining.

QB64, (削除) 102 (削除ここまで) 94 bytes

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""<o$THEN?o$ELSE WRITE i
NEXT

Doesn't work on actual QBasic; see below for why.

This program has one problem: QBasic/QB64 outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" < o$ THEN
 PRINT o$
 ELSE
 WRITE i
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and output o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

The other tricky part is printing numbers according to the spec. QBasic's PRINT command outputs positive numbers with leading spaces, which is occasionally useful but usually just annoying. The WRITE command, however, does not add a leading space--perfect for our purposes here.

1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". In actual QBasic, 0 isn't a legal index and gives Illegal function call; but in QB64, MID$("Fizz",0) happily returns the whole string instead of complaining.

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

QBasic, 102 bytes

Tested on QB64.

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation:

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" = o$ THEN
 PRINT MID$(STR$(i), 2)
 ELSE
 PRINT o$
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and print o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)

The approach with MID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1

The other difficult part is printing numbers according to the spec. QBasic prints positive numbers with leading spaces, which is occasionally useful occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$.


1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". Fortunately, even though 0 isn't a legal index, MID$("Fizz",0) happily returns the whole string instead of complaining.

QBasic, 102 bytes

Tested on QB64.

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation:

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" = o$ THEN
 PRINT MID$(STR$(i), 2)
 ELSE
 PRINT o$
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and print o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)

The approach with MID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1

The other difficult part is printing numbers according to the spec. QBasic prints positive numbers with leading spaces, which is occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$.


1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". Fortunately, even though 0 isn't a legal index, MID$("Fizz",0) happily returns the whole string instead of complaining.

QBasic, 102 bytes

Tested on QB64.

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation:

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" = o$ THEN
 PRINT MID$(STR$(i), 2)
 ELSE
 PRINT o$
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and print o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)

The approach with MID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1

The other difficult part is printing numbers according to the spec. QBasic prints positive numbers with leading spaces, which is occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$.


1 Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". Fortunately, even though 0 isn't a legal index, MID$("Fizz",0) happily returns the whole string instead of complaining.

Added explanation
Source Link
DLosc
  • 40.7k
  • 6
  • 87
  • 142

QBasic, 102 bytes

Tested on QB64.

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed versionUngolfed code and explanation:

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" = o$ THEN
 PRINT MID$(STR$(i), 2)
 ELSE
 PRINT o$
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and print o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)

The approach with comments coming soonMID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1

The other difficult part is printing numbers according to the spec. QBasic prints positive numbers with leading spaces, which is occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$.


1Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". Fortunately, even though 0 isn't a legal index, MID$("Fizz",0) happily returns the whole string instead of complaining.

QBasic, 102 bytes

Tested on QB64.

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed version with comments coming soon.

QBasic, 102 bytes

Tested on QB64.

FOR i=1TO 100
o$=MID$("Fizz",i*5MOD 15)+MID$("Buzz",i*5MOD 25)
IF""=o$THEN?MID$(STR$(i),2)ELSE?o$
NEXT

This program has one problem: QBasic outputs to an 80x24 window, not a terminal, so the results can't be scrolled back. If you run the above code as-is, all you'll see is the lines from 78 onward. To prove that the code does 1 to 100 correctly, you can add the line SLEEP 1 right before NEXT for a 1-second delay on each iteration.

Ungolfed code and explanation:

FOR i = 1 TO 100
 index = 5 * (i MOD 3)
 o$ = MID$("Fizz", index)
 index = 5 * (i MOD 5)
 o$ = o$ + MID$("Buzz", index)
 IF "" = o$ THEN
 PRINT MID$(STR$(i), 2)
 ELSE
 PRINT o$
 END IF
NEXT

On each iteration, we put the appropriate fizzes and buzzes into the string o$, check if it's empty, and print o$ or the number accordingly. The main question is how to get "Fizz" when i is divisible by 3 and "" otherwise. Here are the approaches I tried:

IF i MOD 3THEN o$=""ELSE o$="Fizz"
o$="":IF i MOD 3=0THEN o$="Fizz"
o$=MID$("Fizz",5*(i MOD 3))
o$=MID$("Fizz",i*5MOD 15)

The approach with MID$ is much shorter. This function takes 3 arguments--string, start index, and number of characters--and returns the appropriate substring. When the third argument is omitted, it takes everything from the start index to the end of the string. Here, when i is exactly divisible, the start index is 0 and we get the whole string; otherwise, it's something larger that's past the end of the string, so MID$ gives "".1

The other difficult part is printing numbers according to the spec. QBasic prints positive numbers with leading spaces, which is occasionally useful but usually just annoying. Even converting to a string with STR$ doesn't make a difference. The best solution I've found is to convert to a string and take everything but the first character using MID$.


1Strings are 1-indexed in QBasic--i.e., in the string "abcd", a is at index 1 and d is at index 4. This is why I'm multiplying the mod result by 5: MID$("Fizz",4) gives "z". Fortunately, even though 0 isn't a legal index, MID$("Fizz",0) happily returns the whole string instead of complaining.

Source Link
DLosc
  • 40.7k
  • 6
  • 87
  • 142
Loading

AltStyle によって変換されたページ (->オリジナル) /