4 kyu

Getting along with Integer Partitions

105 of 3,569g964

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.

Fundamentals
Algorithms

More By Author:

Check out these other kata created by g964

Stats:

CreatedAug 15, 2015
PublishedAug 15, 2015
Warriors Trained34167
Total Skips15663
Total Code Submissions29106
Total Times Completed3569
Ruby Completions105
Python Completions1132
JavaScript Completions497
Haskell Completions100
Java Completions397
C# Completions218
CoffeeScript Completions9
Clojure Completions23
C++ Completions355
PHP Completions85
Rust Completions184
C Completions121
Crystal Completions10
Swift Completions55
TypeScript Completions133
Go Completions143
R Completions25
Shell Completions5
OCaml Completions16
F# Completions21
Scala Completions50
Julia Completions17
Nim Completions10
Racket Completions13
Reason Completions3
Fortran Completions4
Dart Completions44
Perl Completions8
Lua Completions21
Pascal Completions7
Prolog Completions5
Total Stars1065
% of votes with a positive feedback rating91% of 602
Total "Very Satisfied" Votes514
Total "Somewhat Satisfied" Votes71
Total "Not Satisfied" Votes17
Total Rank Assessments8
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • g964 Avatar
  • jhoffner Avatar
  • Freywar Avatar
  • Blind4Basics Avatar
  • schoppmp Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
Ad