├── 10.GoldenProgrammerGreatApe.cpp ├── 02.ProgrammerKaioken4.c ├── 11.SuperProgrammer4.java ├── 03.ProgrammerKaioken10.c ├── WiseQuotes.md ├── 04.ProgrammerKaioken20.c ├── 01.BaseProgrammer.c ├── 06.PseudoSuperProgrammer.cpp ├── 05.ProgrammerGreatApe.c ├── 07.SuperProgrammer.cpp ├── 08.SuperProgrammer2.cpp ├── 09.SuperProgrammer3.cpp ├── LICENSE ├── 12.SuperProgrammerGod.java └── README.md /10.GoldenProgrammerGreatApe.cpp: -------------------------------------------------------------------------------- 1 | /// You simply got over-bored... 2 | 3 | bool isPrime(int n, int _=2){ 4 | return _*_>n?11? !new String(new char[n]).matches(".?|(..+?)\\1+"): false; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /03.ProgrammerKaioken10.c: -------------------------------------------------------------------------------- 1 | /// You don't need to calculate all the way to the end, finding one divisor 2 | /// is enough. So, just 'return', no need for 'flag'. 'break' would work too, 3 | /// but 'return' is better. 4 | 5 | int prime_num(int x){ 6 | int i = 2; 7 | while (i < x){ 8 | if (x % i == 0){ 9 | return 0; 10 | } 11 | i++; 12 | } 13 | 14 | return 1; 15 | } 16 | -------------------------------------------------------------------------------- /WiseQuotes.md: -------------------------------------------------------------------------------- 1 | 2 | > 3 | > Real programmers don't read manuals. Reliance on a reference is a hallmark of the novice and the coward. 4 | > 5 | 6 | --- 7 | 8 | > 9 | > If it was hard to write, it should be hard to read. 10 | > 11 | 12 | --- 13 | 14 | What Programmers Say | What Programmers Mean 15 | --- | --- 16 | It's broken | There are bugs in your code 17 | It has a few issues | There are bugs in my code 18 | 19 | -------------------------------------------------------------------------------- /04.ProgrammerKaioken20.c: -------------------------------------------------------------------------------- 1 | /// You have grown SMARTER, and halved the run time by checking 2 | /// divisibility with odd-numbers only. You also understand that 3 | /// looping till x/2 is enough. 4 | 5 | int prime_or_not(int x) { 6 | if (x == 2) { 7 | return 1; 8 | } 9 | 10 | if (x % 2 == 0) { 11 | return 0; 12 | } 13 | 14 | int i; 15 | for (i = 3; i < x/2; i += 2) { 16 | if (x % i == 0){ 17 | return 0; 18 | } 19 | } 20 | 21 | return 1; 22 | } 23 | -------------------------------------------------------------------------------- /01.BaseProgrammer.c: -------------------------------------------------------------------------------- 1 | /// Though u knew how prime number works (of course u r good @ math), 2 | /// it took a long time to come up with the logic. Also, u made several 3 | /// mistakes on the way. And now, u are proud of ur brilliant solution. 4 | /// YES!!! 5 | 6 | int func(int a) 7 | { 8 | int x,i, r; 9 | x = 0; 10 | i = 0 ; 11 | for(i=1; i<=a ; i=i+1 ) 12 | { 13 | if(a%i == 0) 14 | {x=x+1 ;} 15 | } 16 | if( x == 2) 17 | {r = 1;} 18 | else 19 | {r = 0;} 20 | return r ; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /06.PseudoSuperProgrammer.cpp: -------------------------------------------------------------------------------- 1 | /// You understand that, mere comment cannot make bad code look good. 2 | /// So, you get rid of those crappy names plus annoying comments, and make 3 | /// the code truly readable. 4 | 5 | bool primeNumberChecker(int number){ 6 | 7 | if (number == 2){ 8 | return true; 9 | } 10 | 11 | if (number % 2 == 0){ 12 | return false; 13 | } 14 | 15 | int limit = number/3; 16 | for (int factor = 3; factor < limit; factor += 2){ 17 | if (number % factor == 0){ 18 | return false; 19 | } 20 | } 21 | 22 | return true; 23 | } 24 | -------------------------------------------------------------------------------- /05.ProgrammerGreatApe.c: -------------------------------------------------------------------------------- 1 | /// You want everyone to understand your code easily. 2 | /// So, you put some REALLLLY helpful comments. Runtime 3 | /// is cut from n/2 to n/3. 4 | 5 | /* 6 | * checks if a number is prime 7 | */ 8 | int prime_check(int n){ 9 | if (n == 2){ // 2 is prime 10 | return 1; 11 | } 12 | 13 | if (n % 2 == 0){ // if divisible by 2 then not prime 14 | return 0; 15 | } 16 | 17 | int i = 3; // i is odd 18 | int lim = n/3; 19 | for (; i < lim; i += 2){ // check for all odd numbers till n/3 20 | if (n % i == 0){ // if divisible then not prime 21 | return 0; // not prime 22 | } 23 | } 24 | 25 | return 1; // is prime number 26 | } 27 | -------------------------------------------------------------------------------- /07.SuperProgrammer.cpp: -------------------------------------------------------------------------------- 1 | /// You have improved time complexity from n to √n! BRILLIANT!!! 2 | /// You covered boundary cases too. No room for unreliable codes :) 3 | /// Most importantly, who elite uses tab?! 4 | 5 | #include 6 | 7 | bool checkPrime(int number) { 8 | if (number < 2) { 9 | return false; 10 | } 11 | if (number == 2) { 12 | return true; 13 | } 14 | if (number % 2 == 0) { 15 | return false; 16 | } 17 | 18 | int limit = sqrt(number); 19 | for (int factor = 3; factor <= limit; factor += 2) { 20 | if (number % factor == 0) { 21 | return false; 22 | } 23 | } 24 | 25 | return true; 26 | } 27 | -------------------------------------------------------------------------------- /08.SuperProgrammer2.cpp: -------------------------------------------------------------------------------- 1 | /// You have come up with a more clever solution for computing √n. 2 | /// In fact, you don't really need to calculate it. 3 | /// `2 == number` instead of `number == 2` can save you from lots 4 | /// of trouble caused by mistyping one `=` giving compilation error 5 | /// in place of undetectable bug. 6 | 7 | bool isPrime (int number) { 8 | if (2 == number) { 9 | return true; 10 | } 11 | if (2 > number || 0 == number%2) { 12 | return false; 13 | } 14 | 15 | for (int factor = 3; factor*factor <= number; factor += 2) { 16 | if (0 == number%factor) { 17 | return false; 18 | } 19 | } 20 | 21 | return true; 22 | } 23 | -------------------------------------------------------------------------------- /09.SuperProgrammer3.cpp: -------------------------------------------------------------------------------- 1 | /// As we don't need to check divisibility by all even numbers after 2 | /// checking by 2, the same goes for 3. So, now we are checking 3 | /// divisibility by only the odd numbers those are not multiple of 3. 4 | 5 | bool isPrime (int number) { 6 | if (3 >= number) { 7 | if (2 > number) { 8 | return false; 9 | } 10 | return true; 11 | } 12 | 13 | if (0 == (number&1) || 0 == (number%3)) { 14 | return false; 15 | } 16 | 17 | int factor = 5; 18 | while (factor*factor <= number) { 19 | if (0 == number%factor) { 20 | return false; 21 | } 22 | factor += 2; 23 | 24 | if (0 == number%factor) { 25 | return false; 26 | } 27 | factor += 4; 28 | } 29 | 30 | return true; 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Minhas Kamal 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 | -------------------------------------------------------------------------------- /12.SuperProgrammerGod.java: -------------------------------------------------------------------------------- 1 | /// Execution time is more important than memory consumption. 2 | /// And if you can do some preprocessing, then you should. 3 | /// Thus, for smaller integers you have reduced the complexity 4 | /// from √n to 1. 5 | 6 | public class PrimeNumberChecker { 7 | 8 | public static final int MAX_MEMORY_SPACE = 64000000; 9 | public static final int MIN_MEMORY_SPACE = 4; 10 | 11 | private boolean[] primeNumberMarks; 12 | 13 | public PrimeNumberChecker() { 14 | this(MAX_MEMORY_SPACE); 15 | } 16 | 17 | public PrimeNumberChecker(int maxValue) { 18 | if (MAX_MEMORY_SPACE < maxValue) { 19 | maxValue = MAX_MEMORY_SPACE; 20 | } else if (MIN_MEMORY_SPACE > maxValue) { 21 | maxValue = MIN_MEMORY_SPACE; 22 | } 23 | 24 | this.primeNumberMarks = new boolean[maxValue+1]; 25 | markPrimeNumbers(); 26 | } 27 | 28 | private void markPrimeNumbers() { 29 | for (int i = 2; i < primeNumberMarks.length; ++i) { 30 | primeNumberMarks[i] = true; 31 | } 32 | 33 | for (int i = 2; i*i <= primeNumberMarks.length; ++i) { 34 | if (true == primeNumberMarks[i]) { 35 | for (int j = i+i; j < primeNumberMarks.length; j += i) { 36 | primeNumberMarks[j] = false; 37 | } 38 | } 39 | } 40 | 41 | return; 42 | } 43 | 44 | public boolean isPrime(int number) { 45 | if (number < 0) { 46 | return false; 47 | } else if (number < primeNumberMarks.length) { 48 | return primeNumberMarks[number]; 49 | } else { 50 | return checkPrime(number); 51 | } 52 | } 53 | 54 | private boolean checkPrime (int number) { 55 | if (0 == (number%2) || 0 == (number%3)) { 56 | return false; 57 | } 58 | 59 | int factor = 5; 60 | while (factor*factor <= number) { 61 | if (0 == number%factor) { 62 | return false; 63 | } 64 | factor += 2; 65 | 66 | if (0 == number%factor) { 67 | return false; 68 | } 69 | factor += 4; 70 | } 71 | 72 | return true; 73 | } 74 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ProgrammerTransformation 2 | #### The Journey of A *Homo programmer* 3 | So, you have fallen love at the very first sight! And, thank god, it is not one of those imbecile apes. It is rather a species from a completely different kingdom- ***Machinia***. 4 | 5 | 6 | ## Assignment: 7 | Write a program that tells if an integer is prime or not. 8 | 9 | 10 | ## The Journey: 11 | 12 | ### 01. [Base Programmer](01.BaseProgrammer.c) 13 | Though you knew how prime numbers work, it took a long time to come up with this logic. Also, You made several mistakes on the way. But now, you are proud of your brilliant solution. 14 | 15 | ``` BaseProgrammer.c 16 | int func(int a) 17 | { 18 | int x,i, r; 19 | x = 0; 20 | i = 0 ; 21 | for(i=1; i<=a ; i=i+1 ) 22 | { 23 | if(a%i == 0) 24 | {x=x+1 ;} 25 | } 26 | if( x == 2) 27 | {r = 1;} 28 | else 29 | {r = 0;} 30 | return r ; 31 | 32 | } 33 | ``` 34 | 35 | ### 02. [Programmer Kaioken x4](02.ProgrammerKaioken4.c) 36 | You have learned to use flags, and think that they are the coolest. Also, indentation is super important! 37 | 38 | ``` ProgrammerKaioken4.c 39 | int pri_num(int a) { 40 | int i = 0, f = 0; 41 | for (i = 2; i < a; i ++) 42 | if (a % i == 0) f = 1; 43 | 44 | if (f == 0) return 1; 45 | else return 0; 46 | } 47 | ``` 48 | 49 | ### 03. [Programmer Kaioken x10](03.ProgrammerKaioken10.c) 50 | You don't need to calculate all the way to the end; finding one divisor is enough. So, just `return`; no need for 'flag'. `break` would do too, but `return` is better. 51 | 52 | ``` ProgrammerKaioken10.c 53 | int prime_num(int x){ 54 | int i = 2; 55 | while (i < x){ 56 | if (x % i == 0){ 57 | return 0; 58 | } 59 | i++; 60 | } 61 | 62 | return 1; 63 | } 64 | ``` 65 | 66 | ### 04. [Programmer Kaioken x20](04.ProgrammerKaioken20.c) 67 | You have grown SMARTER and have halved the run time by checking divisibility with odd-numbers only. You also understand that looping till 'x/2' is enough. 68 | 69 | ``` ProgrammerKaioken20.c 70 | int prime_or_not(int x) { 71 | if (x == 2) { 72 | return 1; 73 | } 74 | 75 | if (x % 2 == 0) { 76 | return 0; 77 | } 78 | 79 | int i; 80 | for (i = 3; i < x/2; i += 2) { 81 | if (x % i == 0){ 82 | return 0; 83 | } 84 | } 85 | 86 | return 1; 87 | } 88 | ``` 89 | 90 | ### 05. [Programmer Great Ape](05.ProgrammerGreatApe.c) 91 | You want everyone to understand your code easily. So, you put some *REALLLLY* helpful comments. Runtime is cut from 'n/2' to 'n/3'. 92 | 93 | ``` ProgrammerGreatApe.c 94 | /* 95 | * checks if a number is prime 96 | */ 97 | int prime_check(int n){ 98 | if (n == 2){ // 2 is prime 99 | return 1; 100 | } 101 | 102 | if (n % 2 == 0){ // if divisible by 2 then not prime 103 | return 0; 104 | } 105 | 106 | int i = 3; // i is odd 107 | int lim = n/3; 108 | for (; i < lim; i += 2){ // check for all odd numbers till n/3 109 | if (n % i == 0){ // if divisible then not prime 110 | return 0; // not prime 111 | } 112 | } 113 | 114 | return 1; // is prime number 115 | } 116 | ``` 117 | 118 | ### 06. [Pseudo Super Programmer](06.PseudoSuperProgrammer.cpp) 119 | Now, you understand- **mere comment cannot make bad code look good**. So, you get rid of those crappy names and annoying comments. And now, the code is truly readable. 120 | 121 | ``` PseudoSuperProgrammer.cpp 122 | bool primeNumberChecker(int number){ 123 | 124 | if (number == 2){ 125 | return true; 126 | } 127 | 128 | if (number % 2 == 0){ 129 | return false; 130 | } 131 | 132 | int limit = number/3; 133 | for (int factor = 3; factor < limit; factor += 2){ 134 | if (number % factor == 0){ 135 | return false; 136 | } 137 | } 138 | 139 | return true; 140 | } 141 | ``` 142 | 143 | ### 07. [Super Programmer](07.SuperProgrammer.cpp) 144 | You have improved time complexity from 'n' to '√n'! BRILLIANT!!!
145 | You covered boundary cases too: no room for unreliable codes :)
146 | *Most importantly, who elite uses the tab?!* 147 | 148 | ``` SuperProgrammer.cpp 149 | #include 150 | 151 | bool checkPrime(int number) { 152 | if (number < 2) { 153 | return false; 154 | } 155 | if (number == 2) { 156 | return true; 157 | } 158 | if (number % 2 == 0) { 159 | return false; 160 | } 161 | 162 | int limit = sqrt(number); 163 | for (int factor = 3; factor <= limit; factor += 2) { 164 | if (number % factor == 0) { 165 | return false; 166 | } 167 | } 168 | 169 | return true; 170 | } 171 | ``` 172 | 173 | ### 08. [Super Programmer 2](08.SuperProgrammer2.cpp) 174 | You have come up with a more clever solution for computing '√n'. In fact, you don't really need to calculate it!
175 | `2 == number`, instead of `number == 2`, can save you from lots of troubles caused by mistyping one `=` by giving a compilation error in place of an undetectable bug. 176 | 177 | ``` SuperProgrammer2.cpp 178 | bool isPrime (int number) { 179 | if (2 == number) { 180 | return true; 181 | } 182 | if (2 > number || 0 == number%2) { 183 | return false; 184 | } 185 | 186 | for (int factor = 3; factor*factor <= number; factor += 2) { 187 | if (0 == number%factor) { 188 | return false; 189 | } 190 | } 191 | 192 | return true; 193 | } 194 | ``` 195 | 196 | ### 09. [Super Programmer 3](09.SuperProgrammer3.cpp) 197 | As we don't need to check divisibility by all even numbers after checking by 2, the same goes for 3. So now, we are checking divisibility by only the odd numbers that are not multiples of 3. 198 | 199 | ``` SuperProgrammer3.cpp 200 | bool isPrime (int number) { 201 | if (3 >= number) { 202 | if (2 > number) { 203 | return false; 204 | } 205 | return true; 206 | } 207 | 208 | if (0 == (number&1) || 0 == (number%3)) { 209 | return false; 210 | } 211 | 212 | int factor = 5; 213 | while (factor*factor <= number) { 214 | if (0 == number%factor) { 215 | return false; 216 | } 217 | factor += 2; 218 | 219 | if (0 == number%factor) { 220 | return false; 221 | } 222 | factor += 4; 223 | } 224 | 225 | return true; 226 | } 227 | ``` 228 | 229 | ### 10. [Golden-Programmer-Great-Ape](10.GoldenProgrammerGreatApe.cpp) 230 | You simply got over-bored... 231 | 232 | ``` GoldenProgrammerGreatApe.cpp 233 | bool isPrime(int N, int _=2){ 234 | return _*_>N?11?!new String(new char[n]).matches(".?|(..+?)\\1+"):false; 245 | } 246 | } 247 | ``` 248 | 249 | ### 12. [Super Programmer God](12.SuperProgrammerGod.java) 250 | Execution time is more important than memory consumption. And if you can do some preprocessing, then you should. Thus for smaller integers, you have reduced the complexity from √n to 1. 251 | 252 | ``` PrimeNumberChecker.java 253 | public class PrimeNumberChecker { 254 | 255 | public static final int MAX_MEMORY_SPACE = 64000000; 256 | public static final int MIN_MEMORY_SPACE = 4; 257 | 258 | private boolean[] primeNumberMarks; 259 | 260 | public PrimeNumberChecker() { 261 | this(MAX_MEMORY_SPACE); 262 | } 263 | 264 | public PrimeNumberChecker(int maxValue) { 265 | if (MAX_MEMORY_SPACE < maxValue) { 266 | maxValue = MAX_MEMORY_SPACE; 267 | } else if (MIN_MEMORY_SPACE > maxValue) { 268 | maxValue = MIN_MEMORY_SPACE; 269 | } 270 | 271 | this.primeNumberMarks = new boolean[maxValue+1]; 272 | markPrimeNumbers(); 273 | } 274 | 275 | private void markPrimeNumbers() { 276 | for (int i = 2; i < primeNumberMarks.length; ++i) { 277 | primeNumberMarks[i] = true; 278 | } 279 | 280 | for (int i = 2; i*i <= primeNumberMarks.length; ++i) { 281 | if (true == primeNumberMarks[i]) { 282 | for (int j = i+i; j < primeNumberMarks.length; j += i) { 283 | primeNumberMarks[j] = false; 284 | } 285 | } 286 | } 287 | 288 | return; 289 | } 290 | 291 | public boolean isPrime(int number) { 292 | if (number < 0) { 293 | return false; 294 | } else if (number < primeNumberMarks.length) { 295 | return primeNumberMarks[number]; 296 | } else { 297 | return checkPrime(number); 298 | } 299 | } 300 | 301 | private boolean checkPrime (int number) { 302 | if (0 == (number%2) || 0 == (number%3)) { 303 | return false; 304 | } 305 | 306 | int factor = 5; 307 | while (factor*factor <= number) { 308 | if (0 == number%factor) { 309 | return false; 310 | } 311 | factor += 2; 312 | 313 | if (0 == number%factor) { 314 | return false; 315 | } 316 | factor += 4; 317 | } 318 | 319 | return true; 320 | } 321 | } 322 | ``` 323 | 324 | ### 13. [Super Programmer God Blue](#) 325 | ### 14. [Super Programmer Blue Kaioken](#) 326 | ### 15. [Programmer Ultra Instinct](#) 327 | 328 | 329 | ## License 330 | MIT License
ProgrammerTransformation is licensed under MIT License. 331 | --------------------------------------------------------------------------------