My omnium-gatherom of scripts and source code.
at main 129 lines 2.5 kB view raw
1/* ======================================================================== 2 * 3 * Filename: 4 * Description: 5 * Author: 6 * Version: 0.0.1 7 * 8 * ======================================================================== */ 9#include <algorithm> 10#include <cmath> 11#include <cstdint> 12#include <iostream> 13#include <numeric> 14#include <ranges> 15#include <vector> 16 17#include <execution> 18#define PAR std::execution::par, 19 20using u32 = uint32_t; 21namespace ra = std::ranges; 22namespace vi = std::views; 23 24template <typename T> using ref = T&; 25 26auto print(ref<const std::vector<u32>> v) -> void; 27auto println(ref<const std::vector<u32>> v) -> void; 28 29auto decompose(u32 n) -> std::vector<u32>; 30 31auto is_cyclops(u32 n) -> bool; 32auto first_cyclops(size_t n) -> std::vector<u32>; 33 34auto is_disarium(u32 n) -> bool; 35auto first_disarium(size_t n) -> std::vector<u32>; 36 37auto main() -> int 38{ 39 std::cout << std::boolalpha << is_disarium(1) << std::endl; 40 std::cout << std::boolalpha << is_disarium(2) << std::endl; 41 std::cout << std::boolalpha << is_disarium(3) << std::endl; 42 auto const k = first_disarium(18); 43 print(k); 44 return 0; 45} 46 47auto print(ref<const std::vector<u32>> v) -> void 48{ 49 for (auto const& e : v) 50 std::cout << e << ' '; 51 std::cout << std::endl; 52} 53 54auto println(ref<const std::vector<u32>> v) -> void 55{ 56 for (auto const& e : v) 57 std::cout << e << std::endl; 58} 59 60auto decompose(u32 n) -> std::vector<u32> 61{ 62 auto v = std::vector<u32>(floor(log10(n)) + 1u, 0u); 63 64 for (auto& e : v | vi::reverse) 65 e = n % 10, n /= 10; 66 67 return v; 68} 69 70auto is_cyclops(u32 n) -> bool 71{ 72 if (n == 0) 73 return true; 74 auto last_digit = n % 10; 75 auto ctr = 0; 76 while (last_digit != 0) { 77 ++ctr; 78 n /= 10; 79 last_digit = n % 10; 80 } 81 n /= 10; 82 last_digit = n % 10; 83 while (last_digit != 0) { 84 --ctr; 85 n /= 10; 86 last_digit = n % 10; 87 } 88 return n == 0 && ctr == 0; 89} 90 91auto first_cyclops(size_t n) -> std::vector<u32> 92{ 93 auto v = std::vector(n, 0u); 94 auto ctr = 1; 95 for (auto& e : v) { 96 while (!is_cyclops(ctr)) 97 ++ctr; 98 e = ctr; 99 ++ctr; 100 } 101 102 return v; 103} 104 105auto is_disarium(u32 n) -> bool 106{ 107 auto const v = decompose(n); 108 auto ctr = 1u; 109 110 auto const transform = [&ctr](auto val) { 111 return std::pow(val, ctr++); 112 }; 113 114 return std::transform_reduce(PAR v.cbegin(), v.cend(), 0L, std::plus{}, 115 transform) == n; 116} 117 118auto first_disarium(size_t n) -> std::vector<u32> 119{ 120 auto v = std::vector(n, 0u); 121 auto ctr = 1; 122 for (auto& e : v) { 123 while (!is_disarium(ctr)) 124 ++ctr; 125 e = ctr; 126 ++ctr; 127 } 128 return v; 129}