├── LICENSE.md └── README.md /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2018 Dylan Araps 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code golfs 2 | 3 | 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. 4 | 5 | 6 | ## Table of Contents 7 | 8 | 9 | 10 | * [Leap Years (44 bytes)](#leap-years-44-bytes) 11 | * [Fibonacci (34 bytes)](#fibonacci-34-bytes) 12 | * [Fizz Buzz (63 bytes)](#fizz-buzz-63-bytes) 13 | * [Niven/Harshad Numbers (45 bytes)](#nivenharshad-numbers-45-bytes) 14 | * [Prime Numbers (54 bytes)](#prime-numbers-54-bytes) 15 | * [Divisors (55 Bytes)](#divisors-55-bytes) 16 | 17 | 18 | 19 | 20 | ## Leap Years (44 bytes) 21 | 22 | Print all leap years between `1800` and `2400`. 23 | 24 | - Set `$@` to all years between `1800` and `2400` which are divisible by `4`. 25 | - Remove all years divisible by `100` which aren't `2000` and `2400`. 26 | - Print using `history -p` which prints arguments line by line. 27 | 28 | ```sh 29 | set {1804..2400..4} 30 | history -p ${@%?[^04]00} 31 | ``` 32 | 33 | ## Fibonacci (34 bytes) 34 | 35 | Print the Fibonacci sequence up to `31`. 36 | 37 | - Run with `2>/dev/null`. 38 | - Loop 31 times through `a` to `C`. 39 | - `a-C` will appear in the output so `%d` is used to only print integers. 40 | - The initial start value is run as a command (`1`) to set the `$_` variable. 41 | - `$_` is then used in place of a regular variable. 42 | 43 | ```sh 44 | 1 45 | printf %d" 46 | " $[i+=_,_=i-_]{a..C} 47 | ``` 48 | 49 | ## Fizz Buzz (63 bytes) 50 | 51 | Print Fizz Buzz up to 100. 52 | 53 | - Run with `2>/dev/null`. 54 | - Loop over `1-100` 55 | - Run `FizzBuzz$i` as a command to populate `$_`. 56 | - Use a string splice to output the portion of the string needed. 57 | 58 | ```sh 59 | for((;i++<100;)){ 60 | FizzBuzz$i 61 | echo ${_:i%3?i%5?8:4:0:i%15?4:8} 62 | } 63 | ``` 64 | 65 | ## Niven/Harshad Numbers (45 bytes) 66 | 67 | Print Niven/Harshad up to 100. 68 | 69 | ```sh 70 | for((;i++<100;)){((i%(i%10+i/10)))||echo $i;} 71 | ``` 72 | 73 | ## Prime Numbers (54 bytes) 74 | 75 | Print prime numbers up to 100. 76 | 77 | - Loop over `1` to `97`. 78 | - Abuse brace expansion to check divisors. 79 | - Abuse `let`. 80 | - ... 81 | 82 | ```sh 83 | for((;j=i++<97;)){ 84 | let j+=i%{1..97}?0:1,j^3||echo $i 85 | } 86 | ``` 87 | 88 | ## Divisors (55 Bytes) 89 | 90 | Print divisors of 1 to 100. 91 | 92 | - Loop over `1` to `100`. 93 | - Use brace expansion too loop over `1` to `100`. 94 | - Set the divisors to `$@`. 95 | 96 | ```sh 97 | for((;i++<100;)){(set $[i%++j?0:{1..100}];echo ${@#0})} 98 | ``` 99 | --------------------------------------------------------------------------------