Last active
August 29, 2015 14:19
-
-
Save yisiper/db03fabbadb32056f16c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // extracted from https://github.com/shendykurnia/ajaib/blob/master/index.html | |
| // runnable on nodejs | |
| // should be able to optimize more, because slower than php version | |
| var zero_pad = function(n,N) { | |
| var string = n + ''; | |
| var num_zeros = N - (string).length; | |
| for(var j = 0;j < num_zeros;j++) { | |
| string = '0' + string; | |
| } | |
| return string; | |
| }; | |
| var process = function() { | |
| var input = 10; | |
| //console.log(parseInt(input));return; | |
| //console.log(input.length); | |
| for(var i = 0;i < input.length;i++) { | |
| //console.log(input.substr(i,1)); | |
| if(isNaN(input.substr(i,1))) { | |
| console.log('error, bro'); | |
| return; | |
| } | |
| } | |
| var N = parseInt(input); | |
| if(isNaN(N) || (N % 2 != 0)) { | |
| console.log('error, bro'); | |
| return; | |
| } | |
| if(N > 12) { | |
| console.log('kegedean, bro'); | |
| return; | |
| } | |
| var max_sum = Math.pow(10,N / 2) - 1; | |
| var answers = []; | |
| //console.log(Math.floor((max_sum + 1) / 2)); | |
| //for(var i = max_sum;i >= Math.floor((max_sum + 1) / 2);i--) { | |
| for(var i = max_sum;i >= 0;i--) { | |
| //console.log(i); | |
| var power_of_two = Math.pow(i,2); | |
| var power_of_two_string = zero_pad(power_of_two,N); | |
| //console.log(power_of_two_string); | |
| var num_1 = parseInt(power_of_two_string.substr(0,N / 2)); | |
| var num_2 = parseInt(power_of_two_string.substr(N / 2)); | |
| //console.log(power_of_two + '|' + num_1 + '|' + num_2); | |
| if(i == (num_1 + num_2)) { | |
| //console.log(i); | |
| if(answers.indexOf(power_of_two_string) == -1) { | |
| answers.push(power_of_two_string); | |
| } | |
| } | |
| } | |
| answers.sort(); | |
| var string_result = ''; | |
| for(var i = 0;i < answers.length;i++) { | |
| string_result += zero_pad(answers[i],N / 2); | |
| if(i <= answers.length - 2) { | |
| string_result += ', '; | |
| if(i == answers.length - 2) { | |
| string_result += 'dan '; | |
| } | |
| } | |
| } | |
| //console.log(string_result); | |
| }; | |
| process(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ref : https://github.com/shendykurnia/ajaib | |
| // This php version actually does the same as javascript(reference) above. | |
| if (empty($argv[1])) { | |
| print("Missing Argument\n"); | |
| exit(); | |
| } | |
| $input = $argv[1]; | |
| $res[10] = "0000000000, 0000000001, 0023804641, 0300814336, 0493817284, 6049417284, 6832014336, 9048004641, dan 9999800001"; | |
| $res[8] = "00000000, 00000001, 04941729, 07441984, 24502500, 25502500, 52881984, 60481729, dan 99980001"; | |
| $res[6] = "000000, 000001, 088209, 494209, dan 998001"; | |
| $res[4] = "0000, 0001, 2025, 3025, dan 9801"; | |
| $res[2] = "00, 01, dan 81"; | |
| if (array_key_exists($input, $res) && $res[$input] == ajaibSolve($input)) { | |
| echo "Success\n"; | |
| } | |
| // testAjaib($input, $res); | |
| function testAjaib($input, $res) { | |
| for ($i = 0; $i <= $input; $i++) { | |
| if (array_key_exists($i, $res) && $res[$i] == ajaibSolve($i)) { | |
| echo "Digit $i Success\n"; | |
| }elseif (!array_key_exists($i, $res)) { | |
| echo "Cannot find index $i for testing \n"; | |
| }else { | |
| echo "Digit $i Failed\n"; | |
| } | |
| } | |
| } | |
| function ajaibSolve($n) { | |
| // in php, this one should be option out - odd number,alpha character | |
| if ($n %2 != 0 || $n <= 0 || $n > strlen(PHP_INT_MAX)) { | |
| return "just error"; | |
| } | |
| $result = array(); | |
| /* | |
| if there is 8 digit & etc, split into two part | |
| 9999 9999 | |
| */ | |
| $max = pow(10, $n / 2) - 1; | |
| $len = strlen($max); | |
| $div = pow(10, $n / 2); | |
| for($i = 0; $i <= $max ; $i++) { | |
| $pow = pow($i, 2); | |
| $first = floor($pow / $div); | |
| $last = floor($pow % $div); | |
| if ($first + $last == $i ) { //pow($first + $last, 2) == $pow) { | |
| $first_pad = (strlen($first) == $len ) ? $first : str_pad($first, $len, "0", STR_PAD_LEFT) ; | |
| $last_pad = (strlen($last) == $len ) ? $last : str_pad($last, $len, "0", STR_PAD_LEFT); | |
| if ($first . $last_pad == $pow) { | |
| array_push($result, $first_pad . $last_pad); | |
| } | |
| } | |
| } | |
| $count = count($result); | |
| $out = implode(array_slice($result,0, $count - 1), ", "); | |
| if ($count > 1) { | |
| $out = $out . ", dan " . $result[$count - 1]; | |
| } | |
| return $out; | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CHALLENGE DESCRIPTION | |
| Angka 3025 adalah angka yang sangat menarik. Mengapa? Karena kalau kita pisah angka 3025 menjadi 30 dan 25, maka kuadrat dari penjumlahan kedua angka tersebut adalah 3025. Dengan kata lain, (30+25)^2 = 3025 | |
| Agar lebih simple, mari kita sebut angka-angka yang memiliki properti tersebut angka "Ajaib" | |
| Saya ingin mencari lagi angka-angka Ajaib lainnya. Tugas kamu, buatlah sebuah program yang mem-print angka | |
| Ajaib yang memiliki jumlah digit N. | |
| INPUT SAMPLE | |
| /**************************** input.txt *************************/ | |
| /*************************** Start ***************************/ | |
| 2 | |
| /**************************** End *******************************/ | |
| OUTPUT SAMPLE | |
| /*************************** output.txt *************************/ | |
| /*************************** Start ***************************/ | |
| 00, 01, dan 81 | |
| /**************************** End *******************************/ | |
| RULES AND ASSUMPTIONS | |
| Angka Ajaib harus memiliki jumlah digit yang genap, contoh 2, 4, 6, 8, dan seterusnya. | |
| Setiap jawaban dipisahkan oleh sebuah koma dan spasi. | |
| Selalu ada kata "dan" sebelum jawaban yang terakhir. | |
| Tidak perlu kata "dan" apabila jawabannya hanya satu. | |
| Angka 0 di depan harus diperhitungkan, contoh 01 dan 00 adalah jawaban yang valid. | |
| Gunakan algoritma yang paling efisien yang kamu ketahui. | |
| Kompleksitas O(n^2) tidak termasuk jawaban yang memuaskan. | |
| Program yang dibuat harus membaca sebuah file yang berisikan sebuah nomor yang akan dipakai sebagai N. | |
| Program harus print output ke file lain. | |
| Apabila file input terdiri dari lebih dari satu baris, maka tolak dan print error. | |
| Apabila file mengandung huruf maka tolak dan print error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PHP version: PHP 5.6.7 | |
| Nodejs version : v0.12.2 | |
| [s@localhost temp]$ time php ajaib.php 10 && time node ajaib.js | |
| Success | |
| real 0m0.085s | |
| user 0m0.083s | |
| sys 0m0.000s | |
| real 0m0.115s | |
| user 0m0.100s | |
| sys 0m0.017s | |
| [s@localhost temp]$ time php ajaib.php 10 && time node ajaib.js | |
| real 0m0.063s | |
| user 0m0.060s | |
| sys 0m0.000s | |
| real 0m0.129s | |
| user 0m0.110s | |
| sys 0m0.020s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment