codegolf
codegolf copied to clipboard
my bash code golfs
Code golfs
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.
Table of Contents
- Leap Years (44 bytes)
- Fibonacci (34 bytes)
- Fizz Buzz (63 bytes)
- Niven/Harshad Numbers (45 bytes)
- Prime Numbers (54 bytes)
- Divisors (55 Bytes)
Leap Years (44 bytes)
Print all leap years between 1800 and 2400.
- Set
$@to all years between1800and2400which are divisible by4. - Remove all years divisible by
100which aren't2000and2400. - Print using
history -pwhich prints arguments line by line.
set {1804..2400..4}
history -p ${@%?[^04]00}
Fibonacci (34 bytes)
Print the Fibonacci sequence up to 31.
- Run with
2>/dev/null. - Loop 31 times through
atoC. a-Cwill appear in the output so%dis 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}
Fizz Buzz (63 bytes)
Print Fizz Buzz up to 100.
- Run with
2>/dev/null. - Loop over
1-100 - Run
FizzBuzz$ias 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}
}
Niven/Harshad Numbers (45 bytes)
Print Niven/Harshad up to 100.
for((;i++<100;)){((i%(i%10+i/10)))||echo $i;}
Prime Numbers (54 bytes)
Print prime numbers up to 100.
- Loop over
1to97. - Abuse brace expansion to check divisors.
- Abuse
let. - ...
for((;j=i++<97;)){
let j+=i%{1..97}?0:1,j^3||echo $i
}
Divisors (55 Bytes)
Print divisors of 1 to 100.
- Loop over
1to100. - Use brace expansion too loop over
1to100. - Set the divisors to
$@.
for((;i++<100;)){(set $[i%++j?0:{1..100}];echo ${@#0})}