Project Euler:Problem 91 Right triangles with integer coordinates
来源:互联网 发布:红苹果预算软件 编辑:程序博客网 时间:2024/04/30 13:04
The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ.
There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
0 ≤ x1, y1, x2, y2 ≤ 2.
Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed?
先确定P点坐标(x1,y1) 1<=x1,y1<=50
再确定与OP垂直的直线PQ
则PQ上所有整数坐标点分别与P点和O点构成了一个直角三角形且直角位于P点上
OP的斜率为k1=y1/x1 要化简约分一下 ,即分子分母同时除以gcd(x1,y1) k1=dy/dx
PQ的斜率应该为-1/k1=-dx/dy
(1)。考虑PQ上的整数点的Y轴坐标小于P点Y轴坐标的情况:
则位于PQ上的整数点应该是相比较于P点坐标,其X坐标增加dy,Y轴坐标减少dx
则X轴方向,PQ上的整数坐标点个数应该是 (xmax-x1)/dy
Y轴方向,PQ上的整数坐标点个数应该是 y1/dx
综上,PQ上的整数坐标点个数为上面两个数的最小值
(2)。对于PQ上的整数点的Y轴大于P点Y轴坐标的情况:
那部分整数坐标点的个数与P1(y1,x1)的(1)情况是完全相同的
所以对于P和P1这两个关于y=x对称的两个点1<=x1,y1<=50
整数坐标点的个数之和为min( (xmax-x1)/dy,y1/dx)*2+min( (xmax-y1)/dx,x1/dy)*2
最后对于直角位于X轴上,位于Y轴上,位于O顶点这三种情况,个数都为2500
所以最后结果还要加上2500*3
import mathdef gcd(a,b): if a<b: a,b=b,a while b!=0: a=a-b if a <b: a,b=b,a return ares=2500*3for i in range(1,51): for j in range(1,51): k=gcd(i,j) tmp=min(math.floor((50-i)*k/j),math.floor(j*k/i))*2 res=res+tmpprint('res = ',res)
- Project Euler:Problem 91 Right triangles with integer coordinates
- Project Euler:Problem 39 Integer right triangles
- Project Euler-Integer right triangles
- Project Euler:Problem 75 Singular integer right triangles
- projecteuler No.91 Right triangles with integer coordinates
- Euler: Integer right triangles
- Problem 39—Integer right triangles
- Problem 39 Integer right triangles (数学)
- Project Euler Problem 91-100
- Euler Project Problem 6
- project euler problem 11
- Project Euler Problem 81
- Project Euler Problem 60
- Project Euler Problem 59
- Project Euler Problem 58
- Project Euler Problem 57
- Project Euler Problem 16
- Project Euler Problem 30
- 【Maven】Fatal error compiling: invalid target release: 1.7 -> [Help 1]
- 实用程序软件包(关于使用静态函数报错问题“Static function declared but not defined in C+++ "eh.h is only for C++!"报错问题)
- Vector::assign的使用
- iOS - 如何集成支付宝
- 若干有序序列,合并成一个新的有序序列
- Project Euler:Problem 91 Right triangles with integer coordinates
- 大型网站技术架构笔录(1):演化
- strcpy,strncpy http://blog.csdn.net/shutear/article/details/8256096
- 黑马程序员----JAVA基础----IO流_3及反射
- strspn函数和strbrk函数
- qt项目生成exe文件指南
- Cocos2d-x开发教程 第四节 深入Action
- 字符串拷贝和内存拷贝函数
- 78 Subsets