R
Focusing on the next few sumsリストed list of the following numbers (or numbers themselves) that can be constructed in the next sum = 1Next sum = 2 can be constructed, the sorted list of リスト numbers below NNext sum = 3 in...Then, how do you combine them by increasing the next number until the list is empty?This method is more complicated than the method of decomposition to the beta, but it is unnecessary to deリスト the list.However, it is subtle whether the deduction is really reduced or not because it is necessary to deリスト the number list after all. (Not verified)Since y can not be written together, I wrote in Haskell and C++.I didn’t know how to get a list of sorted lists from the beginning, so I’m sorting them straight (but I’m a little small in C++).(ruby is the standard number list. Envy......)Haskell version.import Data.List (sort)
-- 素数の無限リスト。
primes :: [Integer]
primes = 2 : filter (\x -> and $ map ((/= 0) . (mod x))
$ takeWhile (\y -> y * y <= x) primes)
[3, 5 ..]
-- 昇順リスト xs から重複を許して選んだ
-- n 個の要素すべてと currの積がmaxを超えない
-- ような積の全リスト。
-- 関数名は、悩んだ挙句投げやりなものに。
theProducts :: [Integer] -> Integer -> Integer -> Int -> [Integer]
theProducts _ _ curr 0 = [curr]
theProducts (x:xs) max curr n = if (x * curr) > max then []
else theProducts (x:xs) max (x * curr) (n - 1)
++ theProducts xs max curr n
-- max 以下で、次数合計が 1 のもの, 2 のもの... (それぞれソート済)
-- を、空リストが返ってくるまで求め、それらを結合する
-- ことで解とする。
solve :: Integer -> [Integer]
solve max = foldl1 (++) $ takeWhile (/= []) $ map (sort . theProducts primes max 1) [1..]
main :: IO ()
main = print $ solve 100
Result:[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,4,6,9,10,14,15,21,25,26,33,34,35,38,39,46,49,51,55,62,65,69,74,77,82,85,87,91,93,95,8,97,6,9,10,14,15,15,21,25,26,33,34,35,35,38,39,46,49,51,51,51,55,58,58,62,62,65,69,69,74,74,74,74,74,74,74,74,74,74,78,75,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64C++ (C++11) version.sk Haskell version of the beta transplantation just by making the number list . And long......Number of std::set By letting it go, we can do it without sorting.#include <iostream>
#include <set>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <iterator>
using num_t = int;
// max 以下の素数のリストを得る。ただし2は必ず含まれる (手抜き)。
std::list<num_t> get_primes(num_t max)
{
std::list<num_t> primes;
for(num_t n = 3; n <= max; n += 2)
{
bool is_prime = true;
for(auto p : primes)
{
if(p * p > max)
{
break;
}
if(n % p == 0)
{
is_prime = false;
break;
}
}
if(is_prime)
{
primes.push_back(n);
}
}
primes.push_front(2);
return std::move(primes);
}
// 昇順リスト [first, last) から重複を許して
// 選んだ n 個の要素すべてと curr の積が max を
// 超えないような積の集合を得る。
void get_list_products(
std::set<num_t> &products,
std::list<num_t>::const_iterator first,
std::list<num_t>::const_iterator last,
num_t max, num_t curr, int n)
{
if(n <= 0)
{
products.insert(curr);
return;
}
if((first == last) || (curr * *first > max))
{
return;
}
get_list_products(products, first, last, max, curr * *first, n - 1);
get_list_products(products, ++first, last, max, curr, n);
}
int main()
{
const num_t N = 100;
std::vector<num_t> result;
const auto primes = get_primes(N);
for(int n = 1; ; n++)
{
std::set<num_t> products;
get_list_products(products, primes.begin(), primes.end(), N, 1, n);
if(products.empty())
{
break;
}
std::copy(products.begin(), products.end(), std::back_inserter(result));
}
for(auto x : result)
{
std::cout << x << ", ";
}
std::cout << std::endl;
return 0;
}
Result: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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 47 48 49 50 51 52 53 54 55 56 47 48 49 50 51 52 53 54 55