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

#Dyalog APL, (削除) 67 (削除ここまで)(削除) 59 (削除ここまで) 51 bytes

Dyalog APL, (削除) 67 (削除ここまで)(削除) 59 (削除ここまで) 51 bytes

#Dyalog APL, (削除) 67 (削除ここまで)(削除) 59 (削除ここまで) 51 bytes

Dyalog APL, (削除) 67 (削除ここまで)(削除) 59 (削除ここまで) 51 bytes

added 1247 characters in body
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90

#Dyalog APL, (削除) 67 (削除ここまで) 59(削除) 59 (削除ここまで) 51 bytes

And the following link runs n=5 thousands of times and gathers statistics about the frequency of each partition: ⎕rl←0 ⋄ {⍺,⍴⍵}⌸ {⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨ ̈10000⍴5


More improvements, with help from Roger Hui :

  • replace {⍵=0:A⋄B} with ×ばつ⍵:B⋄A}. Signum (×ばつ) returns true for ⍵>0 and false for ⍵=0.

  • replace (+/(?+/)>+\) with +/b<?⊃⌽b←+\, it saves a character

  • use a matrix instead of vector of vectors to compute p: replace ⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬ with ⊃↓(0,⍨⊢⍪⍨1 1⍉+\)⍣⍵⍪1.

×ばつ⍵:a,a∇⍵-a←1++/b<?⊃⌽b←+\⍺↑⊃↓(0,⍨⊢⍪⍨1 1⍉+\)⍣⍵⍪1⋄⍬}⍨ (51 bytes)

test n=5 ; test n=65 ; freq stats

#Dyalog APL, (削除) 67 (削除ここまで) 59 bytes

And the following link runs n=5 thousands of times and gathers statistics about the frequency of each partition: ⎕rl←0 ⋄ {⍺,⍴⍵}⌸ {⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨ ̈10000⍴5

#Dyalog APL, (削除) 67 (削除ここまで) (削除) 59 (削除ここまで) 51 bytes

And the following link runs n=5 thousands of times and gathers statistics about the frequency of each partition: ⎕rl←0 ⋄ {⍺,⍴⍵}⌸ {⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨ ̈10000⍴5


More improvements, with help from Roger Hui :

  • replace {⍵=0:A⋄B} with ×ばつ⍵:B⋄A}. Signum (×ばつ) returns true for ⍵>0 and false for ⍵=0.

  • replace (+/(?+/)>+\) with +/b<?⊃⌽b←+\, it saves a character

  • use a matrix instead of vector of vectors to compute p: replace ⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬ with ⊃↓(0,⍨⊢⍪⍨1 1⍉+\)⍣⍵⍪1.

×ばつ⍵:a,a∇⍵-a←1++/b<?⊃⌽b←+\⍺↑⊃↓(0,⍨⊢⍪⍨1 1⍉+\)⍣⍵⍪1⋄⍬}⍨ (51 bytes)

test n=5 ; test n=65 ; freq stats

shaved off a few characters
Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90

#Dyalog APL, 67(削除) 67 (削除ここまで) 59 bytesp←{⍵,⊂1,⍨+/ ̈⌽⍵↑ ̈⍨⌽⍳⍴⍵}⍣⎕⊢⍬⋄f←{⍵=0:⍬⋄a,a∇⍵-a←{1++/(?+/⍵)>+\⍵}⍺↑⍵⊃p}⍨

p←{⍵,⊂1,⍨+/ ̈⌽⍵↑ ̈⍨⌽⍳⍴⍵}⍣⎕⊢⍬⋄f←{⍵=0:⍬⋄a,a∇⍵-a←{1++/(?+/⍵)>+\⍵}⍺↑⍵⊃p}⍨ (67 bytes)

p is a vector of vectors in which p[n][k] is the number of partitions of n into k summands, or equivalently: the number of partitions with greatest summand k. We build p by starting with the empty vector , reading n (the reads input) and repeatedly applying the following:


The link to tryapl.org runs Some improvements:

  • inline p at the cost of slightly worse (but still good enough) performance

  • in the computation of p rearrange and 1, to save a character

  • turn {1++/(?+/⍵)>+\⍵} into a train with 1+ in front: 1+(+/(?+/)>+\)

  • make f an anonymous function and supply (eval'ed input) as an argument to obtain a complete program

f 5{⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨⎕ (59 bytes)

Test with n=5

Test with n=65

And the following link runs n=5 thousands of times and gathers statistics about the frequency of each partition.: ⎕rl←0 ⋄ {⍺,⍴⍵}⌸ {⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨ ̈10000⍴5

#Dyalog APL, 67 bytesp←{⍵,⊂1,⍨+/ ̈⌽⍵↑ ̈⍨⌽⍳⍴⍵}⍣⎕⊢⍬⋄f←{⍵=0:⍬⋄a,a∇⍵-a←{1++/(?+/⍵)>+\⍵}⍺↑⍵⊃p}⍨

p is a vector of vectors in which p[n][k] is the number of partitions of n into k summands, or equivalently: the number of partitions with greatest summand k. We build p by starting with the empty vector , reading n (the reads input) and repeatedly applying the following:

The link to tryapl.org runs f 5 thousands of times and gathers statistics about the frequency of each partition.

#Dyalog APL, (削除) 67 (削除ここまで) 59 bytes

p←{⍵,⊂1,⍨+/ ̈⌽⍵↑ ̈⍨⌽⍳⍴⍵}⍣⎕⊢⍬⋄f←{⍵=0:⍬⋄a,a∇⍵-a←{1++/(?+/⍵)>+\⍵}⍺↑⍵⊃p}⍨ (67 bytes)

p is a vector of vectors in which p[n][k] is the number of partitions of n into k summands, or equivalently: the number of partitions with greatest summand k. We build p by starting with the empty vector , reading n (the reads input) and repeatedly applying the following:


Some improvements:

  • inline p at the cost of slightly worse (but still good enough) performance

  • in the computation of p rearrange and 1, to save a character

  • turn {1++/(?+/⍵)>+\⍵} into a train with 1+ in front: 1+(+/(?+/)>+\)

  • make f an anonymous function and supply (eval'ed input) as an argument to obtain a complete program

{⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨⎕ (59 bytes)

Test with n=5

Test with n=65

And the following link runs n=5 thousands of times and gathers statistics about the frequency of each partition: ⎕rl←0 ⋄ {⍺,⍴⍵}⌸ {⍵=0:⍬⋄a,a∇⍵-a←1+(+/(?+/)>+\)⍺↑⍵⊃{⍵,⊂⌽1,+/ ̈⍵↑ ̈⍨⌽⍳⍴⍵}⍣⍵⊢⍬}⍨ ̈10000⍴5

Source Link
ngn
  • 15.6k
  • 2
  • 45
  • 90
Loading

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