-
-
Save 0x0dea/2154cc47337d783192d1 to your computer and use it in GitHub Desktop.
Project Euler #8 in C, Clojure, Haskell, JavaScript, Python, Ruby, Shell, and VimL
This file contains 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
#include <stdio.h> | |
int main(void) | |
{ | |
int i, j, cur, max = 0; | |
char num[] = | |
"73167176531330624919225119674426574742355349194934" | |
"96983520312774506326239578318016984801869478851843" | |
"85861560789112949495459501737958331952853208805511" | |
"12540698747158523863050715693290963295227443043557" | |
"66896648950445244523161731856403098711121722383113" | |
"62229893423380308135336276614282806444486645238749" | |
"30358907296290491560440772390713810515859307960866" | |
"70172427121883998797908792274921901699720888093776" | |
"65727333001053367881220235421809751254540594752243" | |
"52584907711670556013604839586446706324415722155397" | |
"53697817977846174064955149290862569321978468622482" | |
"83972241375657056057490261407972968652414535100474" | |
"82166370484403199890008895243450658541227588666881" | |
"16427171479924442928230863465674813919123162824586" | |
"17866458359124566529476545682848912883142607690042" | |
"24219022671055626321111109370544217506941658960408" | |
"07198403850962455444362981230987879927244284909188" | |
"84580156166097919133875499200524063689912560717606" | |
"05886116467109405077541002256983155200055935729725" | |
"71636269561882670428252483600823257530420752963450"; | |
for (i = 0; i <= 995; ++i) { | |
cur = 1; | |
for (j = 0; j < 5; ++j) | |
cur *= num[i + j] - '0'; | |
if (cur > max) | |
max = cur; | |
} | |
printf("%d\n", max); | |
return 0; | |
} |
This file contains 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
(def nums | |
(map #(Integer/parseInt %) | |
(re-seq #"\d" (slurp "../input/8")))) | |
(prn (apply max | |
(for [i (range 996)] | |
(apply * (take 5 (drop i nums)))))) |
This file contains 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
import Data.Char (ord) | |
prod = foldl1 (*) . map (\c -> ord c - 48) | |
main = do | |
input <- readFile "../input/8" | |
let nums = foldl1 (++) $ lines input | |
groups = map (\n -> take 5 $ drop n nums) [0..995] | |
print $ maximum $ map prod groups |
This file contains 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
var fs = require('fs'); | |
var input = fs.readFileSync('../input/8').toString().replace(/\n/g, ''); | |
max = 0; | |
for (var i = 0; i < 996; ++i) { | |
var prod = eval(input.substr(i, 5).replace(/\B/g, '*')); | |
max = prod > max ? prod : max; | |
} | |
console.log(max); |
This file contains 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
with open('../input/8') as f: | |
nums = ''.join(f.read().split('\n')) | |
max = 0 | |
for i in xrange(995): | |
digits = map(int, list(nums[i:i + 5])) | |
product = reduce(lambda a, b: a * b, digits) | |
if product > max: | |
max = product | |
print max |
This file contains 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
p IO.read('../input/8') | |
.delete("\n") | |
.chars | |
.map(&:to_i) | |
.each_cons(5) | |
.map { |c| c.reduce(:*) } | |
.max |
This file contains 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
nums=`paste -sd '' ../input/8` | |
for i in `seq 5 996`; do | |
echo -n $nums | tail -c $i | head -c 5 | | |
sed 's/\B/\n/g' | paste -sd '*' | bc | |
done | sort -n | tail -n 1 |
This file contains 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
normal 20J | |
s/ //g | |
let max = 0 | |
let nums = getline('.') | |
for i in range(996) | |
let prod = nums[i] * nums[i + 1] * nums[i + 2] * nums[i + 3] * nums[i + 4] | |
let max = prod > max ? prod : max | |
endfor | |
normal dd | |
let _ = append(0, max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment