Skip to content

Commit 8c60508

Browse files
added divisors
1 parent 8a0c353 commit 8c60508

File tree

6 files changed

+135
-2
lines changed

6 files changed

+135
-2
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2025 Huawei Technologies Co., Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
@author Toni Boehnlein, Christos Matzoros, Pal Andras Papp, Raphael S. Steiner
17+
*/
18+
#pragma once
19+
20+
#include <cassert>
21+
#include <cmath>
22+
#include <limits>
23+
#include <type_traits>
24+
#include <vector>
25+
26+
namespace osp {
27+
28+
template<typename integral_type>
29+
integral_type intSqrtFloor(integral_type num) {
30+
static_assert(std::is_integral_v<integral_type>);
31+
assert(num > 0);
32+
33+
integral_type sqrt = 1;
34+
integral_type numCopy = num;
35+
while (numCopy >= 4) {
36+
sqrt *= 2;
37+
numCopy /= 4;
38+
}
39+
integral_type power2 = sqrt / 2;
40+
while (power2 > 0) {
41+
integral_type sum = sqrt + power2;
42+
if (sum * sum <= num) {
43+
sqrt = sum;
44+
}
45+
power2 /= 2;
46+
}
47+
48+
return sqrt;
49+
}
50+
51+
52+
template<typename integral_type>
53+
std::vector<integral_type> divisorsList(integral_type num) {
54+
static_assert(std::is_integral_v<integral_type>);
55+
assert(num > 0);
56+
57+
std::vector<integral_type> divs;
58+
59+
integral_type ub = intSqrtFloor<integral_type>(num);
60+
for (integral_type div = 1; div <= ub; ++div) {
61+
if (num % div == 0) {
62+
divs.emplace_back(div);
63+
}
64+
}
65+
for (std::size_t indx = divs.back() * divs.back() == num ? divs.size() - 2U : divs.size() - 1U; indx != std::numeric_limits<std::size_t>::max(); --indx) {
66+
divs.emplace_back(num / divs[indx]);
67+
}
68+
69+
return divs;
70+
}
71+
72+
} // end namespace osp
File renamed without changes.

include/osp/graph_algorithms/directed_graph_top_sort.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ limitations under the License.
2323
#include <random>
2424
#include <vector>
2525

26-
#include "osp/auxiliary/math_helper.hpp"
26+
#include "osp/auxiliary/math/math_helper.hpp"
2727
#include "osp/auxiliary/misc.hpp"
2828
#include "osp/concepts/directed_graph_concept.hpp"
2929
#include "directed_graph_util.hpp"

include/osp/graph_implementations/adj_list_impl/compact_sparse_graph.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
#include <type_traits>
2626
#include <vector>
2727

28-
#include "osp/auxiliary/math_helper.hpp"
28+
#include "osp/auxiliary/math/math_helper.hpp"
2929
#include "osp/concepts/computational_dag_concept.hpp"
3030
#include "osp/concepts/directed_graph_edge_desc_concept.hpp"
3131
#include "osp/concepts/constructable_computational_dag_concept.hpp"

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ _add_test( pebbling_schedule_class )
9393
## auxiliary
9494
_add_test( intpower )
9595

96+
_add_test( divisors )
97+
9698
_add_test( uf_structures )
9799

98100
_add_test( set_operations )

tests/divisors.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2024 Huawei Technologies Co., Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
@author Toni Boehnlein, Benjamin Lozes, Pal Andras Papp, Raphael S. Steiner
17+
*/
18+
19+
#define BOOST_TEST_MODULE IntPower
20+
#include <boost/test/unit_test.hpp>
21+
22+
#include "osp/auxiliary/math/divisors.hpp"
23+
24+
using namespace osp;
25+
26+
BOOST_AUTO_TEST_CASE(IntegerSqrt) {
27+
for (std::size_t root = 1U; root < 200U; ++root) {
28+
for (std::size_t num = root * root; num < (root + 1U) * (root + 1U); ++num) {
29+
BOOST_CHECK_EQUAL(intSqrtFloor(num), root);
30+
}
31+
}
32+
33+
for (int root = 1; root < 300; ++root) {
34+
for (int num = root * root; num < (root + 1) * (root + 1); ++num) {
35+
BOOST_CHECK_EQUAL(intSqrtFloor(num), root);
36+
}
37+
}
38+
}
39+
40+
BOOST_AUTO_TEST_CASE(Divisors) {
41+
for (std::size_t num = 1U; num < 1000U; ++num) {
42+
const std::vector<std::size_t> divs = divisorsList(num);
43+
for (const std::size_t &div : divs) {
44+
std::cout << div << ", ";
45+
BOOST_CHECK_EQUAL(num % div, 0U);
46+
}
47+
std::cout << "\n";
48+
49+
auto it = divs.begin();
50+
for (std::size_t i = 1U; i <= num; ++i) {
51+
if (num % i == 0) {
52+
BOOST_CHECK(it != divs.end());
53+
BOOST_CHECK_EQUAL(i, *it);
54+
++it;
55+
}
56+
}
57+
BOOST_CHECK(it == divs.end());
58+
}
59+
}

0 commit comments

Comments
 (0)