Skip to content

Commit 5ce886b

Browse files
committed
Add C++ solution for counting the number of valid triples that satisfy the square sum condition in problem 1925.
1 parent 3e3976f commit 5ce886b

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution{
2+
public:
3+
int countTriples(int n)
4+
{
5+
int res = 0;
6+
for(int a = 1;a<=n;++a)
7+
{
8+
for(int b = 1;b<=n;++b)
9+
{
10+
int c = sqrt(a*a + b*b+1.0);
11+
if(c<=n&&c*c == a*a + b*b)
12+
{
13+
++res;
14+
}
15+
}
16+
}
17+
return res;
18+
}
19+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def countTriples(self,n:int)->int:
3+
ans = 0
4+
for a in range(1,n+1):
5+
for b in range(1,a):
6+
if a*a+b*b>n*n:
7+
break
8+
c2 = a*a+b*b
9+
if iSqrt(c2)**2 == c2:
10+
ans+=1
11+
12+
return ans*2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool isUgly(int n) {
4+
if(n<= 0)
5+
return false;
6+
while(n%3 == 0)
7+
{
8+
n /=3;
9+
}
10+
while(n%5== 0)
11+
{
12+
n/=5;
13+
}
14+
return (n & (n-1)) == 0;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var factors = []int{2,3,5}
2+
3+
func isUgly(n int) bool {
4+
if n<=0{
5+
return false
6+
}
7+
for _,f := range factors{
8+
for n%f == 0{
9+
n/=f
10+
}
11+
}
12+
return n == 1
13+
}

0 commit comments

Comments
 (0)