Project Euler:Problem 91 Right triangles with integer coordinates

The points P (x1y1) and Q (x2y2) 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 ≤ x1y1x2y2 ≤ 2.

Given that 0 ≤ x1y1x2y2 ≤ 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 math
def gcd(a,b):
    if a<b:
        a,b=b,a
    while b!=0:
        a=a-b
        if a <b:
            a,b=b,a
    return a

res=2500*3

for 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+tmp

print('res = ',res)

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 21:56:51

Project Euler:Problem 91 Right triangles with integer coordinates的相关文章

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 39 Integer right triangles

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. {20,48,52}, {24,45,51}, {30,40,50} For which value of p ≤ 1000, is the number of solutions maximised? #include <iostre

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 67 Maximum path sum II

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (righ

Project Euler:Problem 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9

Project Euler:Problem 78 Coin partitions

Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so p(5)=7. OOOOO OOOO   O OOO   OO OOO   O   O OO   OO   O OO   O   O