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
相关推荐
登山人在路上3 小时前
Nginx三种会话保持算法对比
算法·哈希算法·散列表
写代码的小球3 小时前
C++计算器(学生版)
c++·算法
AI科技星3 小时前
张祥前统一场论宇宙大统一方程的求导验证
服务器·人工智能·科技·线性代数·算法·生活
Fuly10244 小时前
大模型剪枝(Pruning)技术简介
算法·机器学习·剪枝
Xの哲學4 小时前
Linux网卡注册流程深度解析: 从硬件探测到网络栈
linux·服务器·网络·算法·边缘计算
bubiyoushang8884 小时前
二维地质模型的表面重力值和重力异常计算
算法
仙俊红5 小时前
LeetCode322零钱兑换
算法
颖风船5 小时前
锂电池SOC估计的一种算法(改进无迹卡尔曼滤波)
python·算法·信号处理
551只玄猫5 小时前
KNN算法基础 机器学习基础1 python人工智能
人工智能·python·算法·机器学习·机器学习算法·knn·knn算法