Getting along with Integer Partitions
Description:
From wikipedia https://en.wikipedia.org/wiki/Partition_(number_theory)
In number theory and combinatorics, a partition of a positive integer n, also called an integer partition, is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition.
For example, 4 can be partitioned in five distinct ways:
4, 3 + 1, 2 + 2, 2 + 1 + 1, 1 + 1 + 1 + 1
.
We can write:
enum(4) -> [[4],[3,1],[2,2],[2,1,1],[1,1,1,1]]
and
enum(5) -> [[5],[4,1],[3,2],[3,1,1],[2,2,1],[2,1,1,1],[1,1,1,1,1]]
.
The number of parts in a partition grows very fast.
For n = 50 number of parts is 204226
, for 80 it is 15,796,476
It would be too long to tests answers with arrays of such size. So our task is the following:
1 - n being given (n integer, 1 <= n <= 50) calculate enum(n) ie the partition of n.
We will obtain something like that:enum(n) -> [[n],[n-1,1],[n-2,2],...,[1,1,...,1]]
(order of array and sub-arrays
doesn't matter). This part is not tested.
2 - For each sub-array of enum(n) calculate its product. If n = 5 we'll obtain after removing duplicates and sorting:
prod(5) -> [1,2,3,4,5,6]
prod(8) -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 18]
If n = 40 prod(n) has a length of 2699
hence the tests will not verify such arrays.
Instead our task number 3 is:
3 - return the range, the average and the median of prod(n) in the following form (example for n = 5):
"Range: 5 Average: 3.50 Median: 3.50"
Range is an integer, Average and Median are float numbers rounded to two decimal places (".2f" in some languages).
Notes:
Range
: difference between the highest and lowest values.
Mean or Average
: To calculate mean, add together all of the numbers
in a set and then divide the sum by the total count of numbers.
Median
: The median is the number separating the higher half
of a data sample from the lower half.
(https://en.wikipedia.org/wiki/Median)
Hints:
Try to optimize your program to avoid timing out.
Memoization can be helpful but it is not mandatory for being successful.
Similar Kata:
Stats:
Created | Aug 15, 2015 |
Published | Aug 15, 2015 |
Warriors Trained | 34167 |
Total Skips | 15663 |
Total Code Submissions | 29106 |
Total Times Completed | 3569 |
Ruby Completions | 105 |
Python Completions | 1132 |
JavaScript Completions | 497 |
Haskell Completions | 100 |
Java Completions | 397 |
C# Completions | 218 |
CoffeeScript Completions | 9 |
Clojure Completions | 23 |
C++ Completions | 355 |
PHP Completions | 85 |
Rust Completions | 184 |
C Completions | 121 |
Crystal Completions | 10 |
Swift Completions | 55 |
TypeScript Completions | 133 |
Go Completions | 143 |
R Completions | 25 |
Shell Completions | 5 |
OCaml Completions | 16 |
F# Completions | 21 |
Scala Completions | 50 |
Julia Completions | 17 |
Nim Completions | 10 |
Racket Completions | 13 |
Reason Completions | 3 |
Fortran Completions | 4 |
Dart Completions | 44 |
Perl Completions | 8 |
Lua Completions | 21 |
Pascal Completions | 7 |
Prolog Completions | 5 |
Total Stars | 1065 |
% of votes with a positive feedback rating | 91% of 602 |
Total "Very Satisfied" Votes | 514 |
Total "Somewhat Satisfied" Votes | 71 |
Total "Not Satisfied" Votes | 17 |
Total Rank Assessments | 8 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |