aboutsummaryrefslogtreecommitdiff
path: root/challenge-241/deadmarshal/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-241/deadmarshal/cpp')
-rw-r--r--challenge-241/deadmarshal/cpp/ch-1.cpp30
-rw-r--r--challenge-241/deadmarshal/cpp/ch-2.cpp48
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-241/deadmarshal/cpp/ch-1.cpp b/challenge-241/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..54f493de8c
--- /dev/null
+++ b/challenge-241/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,30 @@
+#include<iostream>
+#include<vector>
+
+template<typename T>
+size_t arithmetic_triplets(const std::vector<T> &vec,T diff)
+{
+ size_t count{};
+ for(size_t i = 0; i < vec.size(); ++i)
+ {
+ for(size_t j = i+1; j < vec.size(); ++j)
+ {
+ for(size_t k = j+1; k < vec.size(); ++k)
+ {
+ if((vec.at(j) - vec.at(i) == diff) &&
+ (vec.at(k) - vec.at(j) == diff))
+ count++;
+ }
+ }
+ }
+ return count;
+}
+
+int main(void)
+{
+ std::vector<int> vec1{0,1,4,6,7,10},vec2{4,5,6,7,8,9};
+ std::cout << arithmetic_triplets<int>(vec1,3) << '\n'
+ << arithmetic_triplets<int>(vec2,2) << '\n';
+ return 0;
+}
+
diff --git a/challenge-241/deadmarshal/cpp/ch-2.cpp b/challenge-241/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..b53ab789cb
--- /dev/null
+++ b/challenge-241/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,48 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+template<typename T>
+size_t count_factors(T n)
+{
+ size_t count{};
+ T c = 2;
+ while(n > 1)
+ {
+ if(n % c == 0)
+ {
+ n /= c;
+ count++;
+ }
+ else c++;
+ }
+ return count;
+}
+
+template<typename T>
+void prime_order(std::vector<T> &vec)
+{
+ std::sort(vec.begin(),vec.end(),[&](T a, T b){
+ size_t fa = count_factors<T>(a);
+ size_t fb = count_factors<T>(b);
+ return fa == fb ? a < b : fa < fb;
+ });
+}
+
+template<typename T>
+std::ostream &operator<<(std::ostream &os,
+ const std::vector<T>& vec)
+{
+ for(const auto &e : vec) os << e << ' ';
+ os << "\n";
+ return os;
+}
+
+int main()
+{
+ std::vector<int> vec{11,8,27,4};
+ prime_order<int>(vec);
+ std::cout << vec;
+ return 0;
+}
+