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

deleted 133 characters in body
Source Link
miles
  • 17.3k
  • 2
  • 31
  • 95

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) (削除) 36 (削除ここまで) 35(削除) 35 (削除ここまで) 32 bytes

a:0&1&(][:~.@,(,;}:\./:~@,1+(+{:)&.>~.@,,&1&.>))&>)~]
Partition for n = 1
[[1]]
Partition for n = 2
[[1, 1]] join [[2]]
= [[2][[1, [11], 1]][2]]
Partition for n = 3
[[2[[1, 1]2], [1, 1, 1]] join [[3], [2[1, 1]]2]]
= [[3], [2[1, 1]2], [1, 1, 1]]
... and so on
 f =: a:0&1&(][:~.@,(,;}:\./:~@,1+(+{:)&.>~.@,,&1&.>))&>)~]
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐┌───┬─┐
│2│1│1 1│1│2│
└─┴───┘└───┴─┘
 f 3
┌─┬───┬─────┐┌─────┬───┬─┐
│3│2 1│1│1 1 1│1│1 2│3│
└─┴───┴─────┘└─────┴───┴─┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐┌─────────┬───────┬─────┬───┬─────┬───┬─┐
│5│3 2│2 2 1│2│1 1 1 1│4 1│3 1 1│1 1 1 2│1 2 2│2 3│1 1 1│3│1 4│5│
└─┴───┴─────┴───────┴───┴─────┴─────────┘└─────────┴───────┴─────┴───┴─────┴───┴─┘
 # f 15
176

Since J does not support ragged arrays, each partition has to be boxed so they they won't be zero padded when appended to other partitions. A trick is to use &.> whichs allows you to operate on the items inside a box by opening it, using it, and reboxing it.

a:0&1&(][:~.@,(,;}:\./:~@,1+(+{:)&.>~.@,,&1&.>))&>)~] Input: n
a: The empty box
 ] Get the input n
 0&1&( )~ Repeat n times with an initial array of one empty box
 ( ,&1&.> )&> Append 1Operate toon each boxpartition
 (}:\:~@,1+{:)&.> ( Increment the) last value in each box (Each box is
 Hook a partition
 {. sorted down so that the lastGet valueits ishead (the minimumsmallest value)
 1 {: + Get the tailAdd of1 thatto boxit
 1 1+}. Drop the first Addvalue 1in toeach itpartition
 }: , Get all ofJoin the box exceptprevious thetwo tailresults
 , /:~@ Sort it
 1 Join them
 , \:~@ Prepend a 1 to the initial partition
 Sort it downwards
 ; ,  Box the last Jointwo them
results and join them
 [: , ~.@ TakeFlatten the uniquepairs of boxes
 ]  ~.@ Remove Returnduplicates thoseand boxesreturn
 Return the final result where each box
 is a partition of n

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) (削除) 36 (削除ここまで) 35 bytes

a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)~]
Partition for n = 1
[[1]]
Partition for n = 2
[[1, 1]] join [[2]]
= [[2], [1, 1]]
Partition for n = 3
[[2, 1], [1, 1, 1]] join [[3], [2, 1]]
= [[3], [2, 1], [1, 1, 1]]
... and so on
 f =: a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)~]
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176

Since J does not support ragged arrays, each partition has to be boxed so they they won't be zero padded when appended to other partitions. A trick is to use &.> whichs allows you to operate on the items inside a box by opening it, using it, and reboxing it.

a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)~] Input: n
a: The empty box
 ] Get the input n
 0&( ) Repeat n times with an initial array of one empty box
 ,&1&.> Append 1 to each box
 (}:\:~@,1+{:)&.> Increment the last value in each box (Each box is
 sorted down so that the last value is the minimum)
 {: Get the tail of that box
 1+ Add 1 to it
 }: Get all of the box except the tail
 , Join them
 \:~@ Sort it downwards
 ,  Join them
 ~.@ Take the unique boxes
 ]  Return those boxes
 Return the final result where each box
 is a partition of n

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) (削除) 36 (削除ここまで) (削除) 35 (削除ここまで) 32 bytes

a:1&([:~.@,(,;}./:~@,(+{.))&>)~]
Partition for n = 1
[[1]]
Partition for n = 2
[[1, 1]] join [[2]]
= [[1, 1], [2]]
Partition for n = 3
[[1, 2], [1, 1, 1]] join [[3], [1, 2]]
= [[3], [1, 2], [1, 1, 1]]
... and so on
 f =: a:1&([:~.@,(,;}./:~@,(+{.))&>)~]
 f 1
┌─┐
│1│
└─┘
 f 2
┌───┬─┐
│1 1│2│
└───┴─┘
 f 3
┌─────┬───┬─┐
│1 1 1│1 2│3│
└─────┴───┴─┘
 f 5
┌─────────┬───────┬─────┬───┬─────┬───┬─┐
│1 1 1 1 1│1 1 1 2│1 2 2│2 3│1 1 3│1 4│5│
└─────────┴───────┴─────┴───┴─────┴───┴─┘
 # f 15
176

Since J does not support ragged arrays, each partition has to be boxed so they they won't be zero padded when appended to other partitions.

a:1&([:~.@,(,;}./:~@,(+{.))&>)~] Input: n
a: The empty box
 ] Get the input n
 1&( )~ Repeat n times with an initial array of one empty box
 ( )&> Operate on each partition
 ( ) Hook a partition
 {. Get its head (the smallest value)
 1 + Add 1 to it
 1 }. Drop the first value in each partition
 , Join the previous two results
 /:~@ Sort it
 1 , Prepend a 1 to the initial partition
 ; Box the last two results and join them
 [: , Flatten the pairs of boxes
 ~.@ Remove duplicates and return
 Return the final result where each box
 is a partition of n
added 20 characters in body
Source Link
miles
  • 17.3k
  • 2
  • 31
  • 95

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) 36(削除) 36 (削除ここまで) 35 bytes

a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_~]

Builds the integer partition of n by constructing the integer partitions from 1 to n. Computes the result for n = 15 in millisecondsa millisecond.

 f =: a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_~]
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176
a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_~] Input: n
a: a:"_ The empty box
 [  ] Get the input n
 0&( ^: ) Repeat n times with an initial array of one empty box
 ,&1&.> ,&1&.> Append 1 to each partitionbox
 (}:\:~@,1+{:)&.> Increment the last value in each partition
 box (Each box is
 Each partition is sorted down so that the last value is the minimum)
 {: last value is the minimum
 Get the tail of that box
 {: 1+ Get the tail of the partition
Add 1 to it
 }: 1+ Add 1 toGet it
all of the }:box except the tail
 , Get all of the partition except theJoin tailthem
 , \:~@ Sort Joinit themdownwards
 \:~@  , Sort itJoin downwardsthem
 ,  ~.@ Take the Joinunique themboxes
 ] ~.@ Take theReturn uniquethose partitionsboxes
 ] Return the Returnfinal onlyresult thewhere neweach partitionsbox
 is Returna thepartition finalof resultn

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) 36 bytes

(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_

Builds the integer partition of n by constructing the integer partitions from 1 to n. Computes the result for n = 15 in milliseconds.

 f =: (](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176
(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_ Input: n
 a:"_ The empty box
 [  Get the input n
 ^: Repeat n times ,&1&.> Append 1 to each partition
 (}:\:~@,1+{:)&.> Increment the last value in each partition
  Each partition is sorted down so that the last value is the minimum
 {: Get the tail of the partition
 1+ Add 1 to it
 }: Get all of the partition except the tail
 ,  Join them
 \:~@  Sort it downwards
 ,  Join them
 ~.@ Take the unique partitions
 ] Return only the new partitions
 Return the final result

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) (削除) 36 (削除ここまで) 35 bytes

a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)~]

Builds the integer partition of n by constructing the integer partitions from 1 to n. Computes the result for n = 15 in a millisecond.

 f =: a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)~]
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176
a:0&(](}:\:~@,1+{:)&.>~.@,,&1&.>)~] Input: n
a: The empty box
 ] Get the input n
 0&( ) Repeat n times with an initial array of one empty box
 ,&1&.> Append 1 to each box
 (}:\:~@,1+{:)&.> Increment the last value in each box (Each box is
 sorted down so that the last value is the minimum)
 {: Get the tail of that box
 1+ Add 1 to it
 }: Get all of the box except the tail
 , Join them
 \:~@ Sort it downwards
 , Join them
 ~.@ Take the unique boxes
 ] Return those boxes
 Return the final result where each box
 is a partition of n
added 125 characters in body
Source Link
miles
  • 17.3k
  • 2
  • 31
  • 95

J, (削除) 49 (削除ここまで) 42(削除) 42 (削除ここまで) 36 bytes

3 :'(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:(y-1)<1'[a:"_

It's tacit now!

 f =: 3 :'(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:(y-1)<1'[a:"_
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176
(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:(y-1)<1[a:"_ Input: yn
 a:"_ The empty <1box
 Box the value 1, represents [[1]] [ Get the input n
( )^:(y-1) Repeat y-1n times
 ,&1&.> Append 1 to each partition
 (}:\:~@,1+{:)&.> Increment the last value in each partition
 Each partition is sorted down so that the
 last value is the minimum
 {: Get the tail of the partition
 1+ Add 1 to it
 }: Get all of the partition except the tail
 , Join them
 \:~@ Sort it downwards
 , , Join them
 ~.@ Take the unique partitions
 ]  Return only the new partitions
 Return the final result

J, (削除) 49 (削除ここまで) 42 bytes

3 :'((}:\:~@,1+{:)&.>~.@,,&1&.>)^:(y-1)<1'
 f =: 3 :'((}:\:~@,1+{:)&.>~.@,,&1&.>)^:(y-1)<1'
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176
((}:\:~@,1+{:)&.>~.@,,&1&.>)^:(y-1)<1 Input: y
 <1 Box the value 1, represents [[1]]
( )^:(y-1) Repeat y-1 times
 ,&1&.> Append 1 to each partition
 (}:\:~@,1+{:)&.> Increment the last value in each partition
 Each partition is sorted down so that the
 last value is the minimum
 {: Get the tail of the partition
 1+ Add 1 to it
 }: Get all of the partition except the tail
 , Join them
 \:~@ Sort it downwards
 , Join them
 ~.@ Take the unique partitions
 Return the final result

J, (削除) 49 (削除ここまで) (削除) 42 (削除ここまで) 36 bytes

(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_

It's tacit now!

 f =: (](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_
 f 1
┌─┐
│1│
└─┘
 f 2
┌─┬───┐
│2│1 1│
└─┴───┘
 f 3
┌─┬───┬─────┐
│3│2 1│1 1 1│
└─┴───┴─────┘
 f 5
┌─┬───┬─────┬───────┬───┬─────┬─────────┐
│5│3 2│2 2 1│2 1 1 1│4 1│3 1 1│1 1 1 1 1│
└─┴───┴─────┴───────┴───┴─────┴─────────┘
 # f 15
176
(](}:\:~@,1+{:)&.>~.@,,&1&.>)^:[a:"_ Input: n
 a:"_ The empty box
  [ Get the input n
 ^: Repeat n times
 ,&1&.> Append 1 to each partition
 (}:\:~@,1+{:)&.> Increment the last value in each partition
 Each partition is sorted down so that the
 last value is the minimum
 {: Get the tail of the partition
 1+ Add 1 to it
 }: Get all of the partition except the tail
 , Join them
 \:~@ Sort it downwards
 , Join them
 ~.@ Take the unique partitions
 ]  Return only the new partitions
 Return the final result
added 1334 characters in body
Source Link
miles
  • 17.3k
  • 2
  • 31
  • 95
Loading
deleted 4 characters in body
Source Link
miles
  • 17.3k
  • 2
  • 31
  • 95
Loading
Source Link
miles
  • 17.3k
  • 2
  • 31
  • 95
Loading

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