All of the below code snippets are MIT licensed and written solely by myself. These solutions will no longer be published on any code golfing websites as they are not credible regarding copycats and stolen code.
- Leap Years (44 bytes)
- Fibonacci (34 bytes)
- Fizz Buzz (63 bytes)
- Niven/Harshad Numbers (45 bytes)
- Prime Numbers (54 bytes)
- Divisors (55 Bytes)
Print all leap years between 1800
and 2400
.
- Set
$@
to all years between1800
and2400
which are divisible by4
. - Remove all years divisible by
100
which aren't2000
and2400
. - Print using
history -p
which prints arguments line by line.
set {1804..2400..4}
history -p ${@%?[^04]00}
Print the Fibonacci sequence up to 31
.
- Run with
2>/dev/null
. - Loop 31 times through
a
toC
. a-C
will appear in the output so%d
is used to only print integers.- The initial start value is run as a command (
1
) to set the$_
variable. $_
is then used in place of a regular variable.
1
printf %d"
" $[i+=_,_=i-_]{a..C}
Print Fizz Buzz up to 100.
- Run with
2>/dev/null
. - Loop over
1-100
- Run
FizzBuzz$i
as a command to populate$_
. - Use a string splice to output the portion of the string needed.
for((;i++<100;)){
FizzBuzz$i
echo ${_:i%3?i%5?8:4:0:i%15?4:8}
}
Print Niven/Harshad up to 100.
for((;i++<100;)){((i%(i%10+i/10)))||echo $i;}
Print prime numbers up to 100.
- Loop over
1
to97
. - Abuse brace expansion to check divisors.
- Abuse
let
. - ...
for((;j=i++<97;)){
let j+=i%{1..97}?0:1,j^3||echo $i
}
Print divisors of 1 to 100.
- Loop over
1
to100
. - Use brace expansion too loop over
1
to100
. - Set the divisors to
$@
.
for((;i++<100;)){(set $[i%++j?0:{1..100}];echo ${@#0})}