Project Euler 75:Singular integer right triangles
原题:
It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed?
翻译:
唯一的整数边直角三角形
只能唯一地弯折成整数边直角三角形的电线最短长度是12厘米;当然,还有很多长度的电线都只能唯一地弯折成整数边直角三角形,例如:
12厘米: (3,4,5)
24厘米: (6,8,10)
30厘米: (5,12,13)
36厘米: (9,12,15)
40厘米: (8,15,17)
48厘米: (12,16,20)
相反地,有些长度的电线,比如20厘米,不可能弯折成任何整数边直角三角形,而另一些长度则有多个解;例如,120厘米的电线可以弯折成三个不同的整数边直角三角形。
120厘米: (30,40,50), (20,48,52), (24,45,51)
记电线长度为L,对于L ≤ 1,500,000,有多少种取值只能唯一地弯折成整数边直角三角形?
解题思路:
先参看维基百科,如下:

对正整数m、n,且m>n

若a 、b、c能构成直角三角形 ,则当且仅当:m和n互质,m-n是奇数
同时a、b、c乘以k的整数倍也能够成直角三角形。
解题方法就很明显的
先考虑m的取值范围
a、b是直角边、c是斜边,极端情况下:a=c=L/2,b=0,则n=0,m2 =L/2,则m = sqrt(L/2)
这样在依靠上面的公式即可
Java程序:
package Level3;
public class PE075{
void run(){
int L = 1500000;
int max_m = (int)Math.sqrt(L/2);
int[] triple = new int[L+1];
int a,b,c;
int s;
for(int m=2;m<=max_m;m++){
for(int n=1;n<m;n++){
if(gcd(m,n)==1 && (m+n)%2==1){
a = m*m-n*n;
b = 2*m*n;
c = m*m+n*n;
s = a+b+c;
// if(a*a+b*b==c*c){
while(s<=L){
triple[s]+=1;
s+=a+b+c;
}
// }
}
}
}
int count=0;
for(int i=2;i<=L;i++)
if(triple[i]==1)
count++;
System.out.println(count);
}
int gcd(int m,int n){
int tmp;
if(m<n){
tmp=m;
m=n;
n=tmp;
}
while(n!=0){
m = m - n;
if(m<n){
tmp = m;
m = n;
n = tmp;
}
}
return m;
}
public static void main(String[] args){
long t0 = System.currentTimeMillis();
new PE075().run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
}
}
运行结果:
161667
running time=0s72ms
Python程序:
import math
import time
def gcd(m,n):
if m<n:
tmp = n
n = m
m = tmp
while n:
m = m%n
if m<n:
tmp = n
n = m
m = tmp
return m def PE075():
L = 1500000
count = 0
max_m = int(math.sqrt(L/2))
triple = [0 for i in range(0,L+1)]
for m in range(2,max_m+1):
for n in range(1,m):
if gcd(m,n)==1 and (m+n)%2==1:
a = m*m-n*n
b = 2*m*n
c = m*m+n*n
s = a+b +c
while s<=L:
triple[s] +=1
if triple[s]==1:
count+=1
if triple[s]==2:
count-=1
s+= a+b+c
return count if __name__=='__main__':
t0 = time.time()
print "result={0},running time={1}s".format(PE075(),(time.time()-t0))
运行结果:
result=161667,running time=1.09399986267s
Project Euler 75:Singular integer right triangles的更多相关文章
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
- Python练习题 044:Project Euler 016:乘方结果各个数值之和
本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...
- Python练习题 043:Project Euler 015:方格路径
本题来自 Project Euler 第15题:https://projecteuler.net/problem=15 ''' Project Euler: Problem 15: Lattice p ...
- Python练习题 042:Project Euler 014:最长的考拉兹序列
本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest C ...
随机推荐
- js设计模式(6)---适配器模式
0.前言 脖子又开始痛了,难道还没成为码农就开始出现颈椎问题,一直以来举得自己不算那种死宅的人,怎么这么年轻就出现这种问题.哎,不管了,还是先把自己学习的适配器模式写出来,算是一种总结吧. 1.为什么 ...
- PHP获取上个月、下个月、本月的日期(strtotime(),date())
今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计当时太困了吧,再看到有种毛骨悚然的感觉,本来是想再处理一下的,但是一想 ...
- Apache,添加虚拟目录
这几天在自己的虚拟机里面安装了2003,有人说window陪iis较好,但是对iis无兴趣,就自己装了apache,下面介绍如何配置自己的虚拟目录. 想配置自己的虚拟目录,那必须要知道点apache的 ...
- .NET SDK和下载
http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx .NET SDK和下载 您可以通过下载.NET框架针对包和软件开发工具包,并使用它们与Visual ...
- hdu 5719 BestCoder 2nd Anniversary B Arrange 简单计数问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5719 题意:一个数列为1~N的排列,给定mn[1...n]和mx[1...n],问有符合的排列数为多少 ...
- EditorWindow 和MenuItem
using UnityEngine; using System.Collections; using UnityEditor; public class ClipEventEditor : Edito ...
- Java程序员使用的20几个大数据工具
最近我问了很多Java开发人员关于最近12个月内他们使用的是什么大数据工具. 这是一个系列,主题为: 语言 web框架 应用服务器 SQL数据访问工具 SQL数据库 大数据 构建工具 云提供商 今天我 ...
- ruby 格式化当前日期时间
ruby 格式化当前日期时间 ruby 用Time类获取当前时间. t = Time.new puts t 可以看到输出的是(我现在运行的时间): Sat Jan 29 10:45:22 +0800 ...
- mvc异步表单遇到的问题
1,mvc异步表单遇到的问题 问题:使用jqury easy ui 时提交异步数据不能请求到服务器 解决办法:经过细心调试和检测,发现jqury的加载顺序放在了easy ui之后,所以首先加 ...
- AFNetworking VS ASIHTTPRequest
AFNetworking和ASIHTTPRequest,大致如下: 使用上:AFN是用上较ASI略简单,但扩展不如ASI;AFN能按普通的block写法直接用闭包的写法,但是ASI不行,这样ASI的代 ...