Done puzzle 64
[project-euler.git] / euler39.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 13,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "false"
14 ]
15 },
16 "execution_count": 13,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "require 'set'"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 27,
28 "metadata": {
29 "collapsed": false
30 },
31 "outputs": [
32 {
33 "data": {
34 "text/plain": [
35 "1..1000"
36 ]
37 },
38 "execution_count": 27,
39 "metadata": {},
40 "output_type": "execute_result"
41 }
42 ],
43 "source": [
44 "results = Hash.new {|x| Set.new}\n",
45 "(1..1000).each do |p|\n",
46 " (1..(p/3)).each do |a|\n",
47 " (1..((p-a)/2)).each do |b|\n",
48 " c = p-(a+b)\n",
49 " if a ** 2 + b ** 2 == c ** 2\n",
50 " # puts \"p = #{p}, a, b, c: #{a} #{b} #{c}\"\n",
51 " results[p] = results[p].add(Set.new [a, b, c])\n",
52 " end\n",
53 " end\n",
54 " end\n",
55 "end"
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": 28,
61 "metadata": {
62 "collapsed": false
63 },
64 "outputs": [
65 {
66 "data": {
67 "text/plain": [
68 "3"
69 ]
70 },
71 "execution_count": 28,
72 "metadata": {},
73 "output_type": "execute_result"
74 }
75 ],
76 "source": [
77 "results[120].length"
78 ]
79 },
80 {
81 "cell_type": "code",
82 "execution_count": 29,
83 "metadata": {
84 "collapsed": false
85 },
86 "outputs": [
87 {
88 "data": {
89 "text/plain": [
90 "840"
91 ]
92 },
93 "execution_count": 29,
94 "metadata": {},
95 "output_type": "execute_result"
96 }
97 ],
98 "source": [
99 "results.keys.max_by {|p| results[p].length}"
100 ]
101 },
102 {
103 "cell_type": "code",
104 "execution_count": null,
105 "metadata": {
106 "collapsed": true
107 },
108 "outputs": [],
109 "source": []
110 }
111 ],
112 "metadata": {
113 "kernelspec": {
114 "display_name": "Ruby 2.4.0",
115 "language": "ruby",
116 "name": "ruby"
117 },
118 "language_info": {
119 "file_extension": ".rb",
120 "mimetype": "application/x-ruby",
121 "name": "ruby",
122 "version": "2.4.0"
123 }
124 },
125 "nbformat": 4,
126 "nbformat_minor": 0
127 }