#!/usr/bin/env python3 ######################################################################## # Solves problem 197 from projectEuler.net. # Finds the sum of 2 values of a recursively defined sequence. # Copyright (C) 2011 Santiago Alessandri # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # You can contact me at san.lt.ss@gmail.com # Visit my blog at http://blog.san-ss.com.ar # Visit my wiki at http://wiki.san-ss.com.ar ######################################################################## from itertools import count, takewhile is_square = lambda x: int(x ** 0.5) ** 2 == x if __name__ == '__main__': for a in count(6): a_2 = a ** 2 for f in (f for f in takewhile(lambda f: f < a, count(4)) if is_square(a_2 - f ** 2)): f_2 = f ** 2 c_2 = a_2 - f_2 setoff = 3 if (f & 1) else 2 for e in (e for e in takewhile(lambda e: e ** 2 < c_2, count(setoff, 2)) if is_square(c_2 - e ** 2) and is_square(a_2 - e ** 2)): e_2 = e ** 2 b_2 = c_2 - e_2 d_2 = a_2 - e_2 z = -(d_2 - c_2) // 2 y = -(-d_2 - c_2 + 2 * b_2) // 2 x = (d_2 + c_2) // 2 print('The result is: (x){0} + (y){1} + (z){2} = {3}'.format(x, y, z, x + y + z)) exit(0)