[Codewars] Integers: Recreation One

Integers: Recreation One

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;

class SumSquaredDivisors
{
public:
static std::vector<std::pair<long long, long long>> listSquared(long long m, long long n);
};
long long get(long long x) {
long long res = 0;
for(long long i = 1; i * i <= x; i++) {
if(x % i) continue;
res += i * i;
if(i * i != x) res += (x / i) * (x / i);
}
return res;
}
vector<pair<long long, long long>> SumSquaredDivisors::listSquared(long long m, long long n) {
vector<pair<long long, long long>> res;
for(long long i = m; i <= n; i++) {
long long squareSum = get(i);
long long sq = sqrt(squareSum);
if(sq * sq == squareSum) res.push_back({i,squareSum});
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/05/PS/Codewars/integers-recreation-one/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.