Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#MATL , 20 bytes

MATL , 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Implicitly input N. Push [1 2 ... N]. These are the possible values of k,
 % except with N instead of 0
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces a 2D array with
 % each combination on a row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % Implicitly end for and display all arrays in the stack

#MATL , 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Implicitly input N. Push [1 2 ... N]. These are the possible values of k,
 % except with N instead of 0
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces a 2D array with
 % each combination on a row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % Implicitly end for and display all arrays in the stack

MATL , 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Implicitly input N. Push [1 2 ... N]. These are the possible values of k,
 % except with N instead of 0
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces a 2D array with
 % each combination on a row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % Implicitly end for and display all arrays in the stack
added 8 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % TakeImplicitly input N. Push [1 2 ... N]. These are the possible values of k, except
 % except with 0N replacedinstead byof N0
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces ana 2D array with each
 % each combination on a different row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % EndImplicitly end for. Implicitlyand display all arrays in the stack

#MATL, 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Take input N. Push [1 2 ... N]. These are the possible values of k, except
 % with 0 replaced by N
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces an array with each
 % combination on a different row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % End for. Implicitly display all arrays in the stack

#MATL, 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Implicitly input N. Push [1 2 ... N]. These are the possible values of k,
 % except with N instead of 0
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces a 2D array with
 % each combination on a row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % Implicitly end for and display all arrays in the stack
added 1723 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

#MATL, 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Take input N. Push [1 2 ... N]. These are the possible values of k, except
 % with 0 replaced by N
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces an array with each
 % combination on a different row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % End for. Implicitly display all arrays in the stack

#MATL, 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

#MATL, 20 bytes

:"0Gq:@XNG3$Yc!dS!Xu

Try it online!

For input 15 it takes about 2 seconds in the online compiler.

###Explanation

This works by generating partition points and then converting to partition lengths. What I mean by this is the following. Given input N = 5, a possible partition is [2 2 1]. This is represented by partition points [0 2 4 5], such that consecutive differences (or lengths) of the partition points give the resulting partition of the input number.

All arrays of partition points start with 0 and end with N. The number k of intermediate points varies from 0 to N-1. For N and k given, the intermediate points can be generated as a combination of the numbers [1, 2, ..., N-1] taken k at a time.

Several arrays of partition points may give rise to the same result in a different order. For example, partition points [0 1 3 5] would give the partition lengths [1 2 2], i.e. the same as the previous [2 2 1] only in a different order. This has to be taken into account by sorting each array of partition lengths and removing duplicates.

: % Take input N. Push [1 2 ... N]. These are the possible values of k, except
 % with 0 replaced by N
" % For each
 0 % Push 0
 Gq: % Push [1 ... N-1]. These the possible intermediate points
 @XN % Push k and produce the combinations. Each k produces an array with each
 % combination on a different row. The value k=N produces an empty array
 G % Push N
 3$Yc % Prepend a column of zeros and append a column of N to the array
 !d % Transpose. Consecutive differences of each column
 S! % Sort each column. Transpose
 Xu % Keep only unique rows
 % End for. Implicitly display all arrays in the stack
edited body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading
added 1 character in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading

AltStyle によって変換されたページ (->オリジナル) /