This is a game where you must count to number from 0 to 1000. However, if the number has a 6 in it, or is divisible by 6, you don't say the number. Instead, you say "Shaggy".
Rules:
- You can't hard-code the numbers.
- The number only has to satisfy at least 1 of the following requirements
- Divisible by 6
- Number contains a 6
- Some type of separator is mandatory (12345Shaggy7.. doesn't count)
- You must count exactly to 1000 from 1.
- The numbers must be output, but it doesn't matter how (e.g., stdout, writing to a text file, etc.).
Solutions
Jump to C, Swift, JavaScript, PHP Solution.
You can submit your own solution in comment section in same/different programming language. If your solution is better or in different language we'll add here.
C
#include <stdio.h>
int main()
{
for(int i=1;i<=1000;++i)
{
if(i%6)
{
for(int m=i;(m>0);m/=10) if (m%10==6) goto l;
printf("%d,",i);
continue;
}
printf("Shaggy,");
}
return 0;
}
Swift
import Foundation
for i in 1...1000 {
print(i%6<1 || (String(i).range(of: "6") != nil) ? "Shaggy" : "\(i)")
}
JavaScript
<html>
<body>
<p id="print"></p>
<script>
for(i=0;i++<1000;)
document.getElementById('print').innerHTML += i%6<1|/6/.test(i)?'Shaggy\n':i+'\n';
</script>
</body>
</html>
PHP
<?
for(;1000>$i++;)
echo$i%6*!strpbrk($i,6)?$i:Shaggy,"\n";