Leetcode 1925. Count Square Sum Triples

Problem

A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2.

Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.

Algorithm

Count the number of Pythagorean triples by enumerating them according to the definition.

Code

python3 复制代码
class Solution:
    def countTriples(self, n: int) -> int:
        cnts = 0
        for a in range(2, n):
            for b in range(2, n):
                c_2 = a * a + b *b
                c = int(sqrt(c_2))
                if c <= n and c * c == c_2:
                    cnts += 1
        return cnts
相关推荐
CS创新实验室31 分钟前
从盘边到芯端——硬盘接口七十年变迁史
算法·磁盘调度
xvhao20131 小时前
单源、多源最短路
数据结构·c++·算法·深度优先·动态规划·图论·图搜索算法
MATLAB代码顾问1 小时前
多种群协同进化算法(MPCE)求解大规模作业车间调度问题——附MATLAB代码
开发语言·算法·matlab
FQNmxDG4S1 小时前
JVM内存模型详解:堆、栈、方法区与垃圾回收
java·jvm·算法
We་ct2 小时前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·leetcode·typescript·动态规划
AI科技星2 小时前
精细结构常数α作为SI 7大基本量纲统一耦合常数的量子几何涌现理论
算法·机器学习·数学建模·数据挖掘·量子计算
txzrxz2 小时前
动态规划——背包问题
算法·动态规划
Yingye Zhu(HPXXZYY)2 小时前
洛谷 P15553 [CCPC 2025 哈尔滨站] 液压机
算法
谭欣辰3 小时前
LCS(最长公共子序列)详解
开发语言·c++·算法
m0_629494733 小时前
LeetCode 热题 100-----17.缺失的第一个正数
数据结构·算法·leetcode