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 ...
随机推荐
- .Net中的Socket通讯
.NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是: ·Socket类 这个低层的类用于管理连接,WebReq ...
- TDirectory.GetDirectories 获取指定目录下的目录
使用函数: System.IOUtils.TDirectory.GetDirectories 所有重载: class function GetDirectories(const Path: strin ...
- 获取枚举Description的Name
/// <summary> /// 获取枚举Description的Name /// </summary> /// <param name="enumName& ...
- python杂记-2(python之文件)
文件打开函数:f = open 表1-1:open函数中模式参数常用值 打开模式 描述 'r' 读模式 'w' 写模式 'a' 追加模式 'b' 二进制模式 '+' 读/写模式 表1-2:文件对象方法 ...
- django 更新model
修改models.py 中对应的class 在admin.py 中 增加 admin.site.register(WafDevice) 进入dbshell python manage.py dbshe ...
- 进程和cpu绑定
#include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/sysinfo ...
- 【jquery】 API讲解 内部培训资料
资料在百度云盘 一.jquery API讲解 1.jquery api如何使用 jquery api http://www.hemin.cn/jq/ 2.常用api讲解 选择器: 通过$()获取 ...
- 从零开始学ios开发(十二):Table Views(上)
这次学习的控件非常重要且非常强大,是ios应用中使用率非常高的一个控件,可以说几乎每个app都会使用到它,它就是功能异常强大的Table Views.可以打开你的iphone中的phone.Messa ...
- C# 清楚Cookies
//销毁Cookies中的数据 if (Request.Cookies["Ticket"] != null) { HttpCookie mycookie; mycookie = R ...
- 结构体,公用体,枚举类型的sizeof
1)枚举类enum型空间计算 enum只是定义了一个常量集合,里面没有“元素”,而枚举类型是当做int来存储的,所以枚举类型的sizeof值都为4 enum color(red,pink,white, ...