Q:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
A:
#! /usr/bin/env python
#coding=utf-8
import math
import time
''' method 1
a,b,c 的范围。abc之间必然存在a<b<c这样类似的关系,假设大小按照这样
显然a<333 & 333<b<500。a+b>c(三角形),所以c < 500
'''
t1 = time.time()
l = []
s = 0
for i in range(334,500):
l.append(i ** 2)
for a in range(1,333):
for b in range(a,500):
s = a ** 2 + b ** 2
if s in l:
c = math.sqrt(s)
if (a + b + c) == 1000:
print "a*b*c = %d*%d*%d = %d" %(a,b,c,a*b*c)
break
t2 = time.time()
print "time used %s" %str(t2-t1)
''' method 2
逻辑有一点难理解,把c^2 - b^2 的值作为key,这个值的开方作为value
(b,b^2) = (c,c^2)
c++
这样key的值如果存在的话,就是满足a^2+b^2=C^2
再判断是否是符合a+b+c=1000
faster than method 1
'''
def squares():
i = 1
while i < 1000:
yield (i,i**2)
i += 1
prev = []
seen = {}
found = False
for (i,isq) in squares():
for (j, jsq) in prev:
k = seen.get(isq-jsq)
if not k: continue
if i + j + k == 1000:
print "a*b*c = %d*%d*%d = %d" %(j,k,i,j*k*i)
found = True
break
if found:break
prev.append((i,isq))
seen[isq] = i
t3 = time.time()
print "time used: %s" %(t3-t2)
分享到:
相关推荐
•Find the lowest, across all departments, of the per-department max- imum salary computed by the preceding query. Answer: •SQL query: select name from student natural join takes natural join course ...
07_driver.page_source.find示例.py07_driver.page_source.find示例.py07_driver.page_source.find示例.py07_driver.page_source.find示例.py07_driver.page_source.find示例.py07_driver.page_source.find示例.py07...
08_driver.find查找元素示例.py08_driver.find查找元素示例.py08_driver.find查找元素示例.py08_driver.find查找元素示例.py08_driver.find查找元素示例.py08_driver.find查找元素示例.py08_driver.find查找元素示例...
在IT领域,尤其是在Java编程中,遇到“Can not find the main class. Program will exit.”这一错误信息是非常常见的问题。这通常意味着编译器或运行环境无法识别并定位到程序的主类,即包含main方法的类,从而导致...
Find the shortest path from 0 to 15 for the following graph. A multistage graph is a graph (1) G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E, then a is in Vi , ...
### "Could Not Find the Main Class, Program Will Exit" 错误解析及解决方法 在Java编程过程中,我们经常会在开发环境中遇到各种各样的错误提示,其中“Could not find the main class, program will exit”是一...
异常详细信息: EnterpriseDT.Net.Ftp.FTPException: plan/??_SV_201011121502.DNJH: The filename, directory name, or volume label syntax is incorrect
在实际应用中,我们经常需要为用户提供查找(Find)和替换(Replace)的功能,以便他们能快速定位到特定的文本内容。`richtextbox1.Find`方法就是实现这一功能的关键。 `richtextbox1.Find`方法是`richtextbox`控件...
"解决 MyEclipse 8.5 中的 “Could not find the main class” 问题" "Could not find the main class" 是一个常见的错误提示,出现于 MyEclipse 8.5 中使用外带的 Tomcat 时,因 JDK 版本问题所引起。下面将详细...
By bringing together crucial information that was previously scattered or difficult to find, Perkins has created an incredible resource that enables professionals to leverage RTP's benefits in a wide ...
标题"findpeaks.zip_The Spectrum_findpeaks_peak picking"揭示了这个压缩包是关于在光谱分析中寻找峰值的工具或代码。光谱分析通常用于物理学、化学、工程学以及计算机科学中的各种应用,例如声音信号处理、图像...
【标题】"POJ1426-Find The Multiple【BFS+同余模】"是一道来源于北京大学在线编程平台POJ的算法题目,主要涉及到了广度优先搜索(BFS)与同余模运算的知识点。这道题目要求解决的是寻找一个整数的倍数问题,可能...
slow to download, and difficult to maintain, but like the badly taught driver who insists on flouting the rules because it。ッs worked for him so far, many developers find those outdated habits ...
Find all employees in the database who live in the same cities and on the same streets as do their managers. d. Find all employees who earn more than the average salary of all employees of their ...
The target audience is developers somewhat familiar with Perl and the Unix-based Web server environment, but you'll find the book illustrative even if your knowledge is sketchy. After showing how to...
The average person consumes approximately 30 gigabytes of data every single day, but has no idea how to interpret it correctly. EVERYDATA explains, through the eyes of an expert economist and ...
这个“find_path4.cpp.zip_The Point”项目似乎专注于解决这个问题。让我们深入探讨这个过程涉及的关键知识点。 首先,`find_path4.cpp` 文件很可能是一个用C++编写的程序,它包含了寻找三维网格(或称为多边形网格...
有的时候在开发jsp时,需要使用jstl时,在jsp上面引用jstl却出现错误:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core,这是由于缺少两个jar包导致的。
Leveraging visual tools, The Startup Equation leads you step by step through the decisions you need to make to find your perfect startup model. You'll learn things such as identifying your unique ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...