Skip to main content
shortened it slightly
Source Link
marinus
  • 31.3k
  • 7
  • 73
  • 112

APL (124 120120 116)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄⎕←(S⍴'⋄S↓'-'),,A/A⊂⍨0≠A←⍨∨\×A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+'⊣S←1≠+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+S←1≠+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negativepositive.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃⍴E←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • '-',A/A⊂⍨0≠A⍨∨\×A: Remove any leading zeroes from the result vector, and add a minus in front.
  • ⎕←(S⍴'-'),S↓: Output itRemove S characters from the front, prefixed with a minusi.e. if the result wasis supposed to be negativepositive then remove the minus again.

APL (124 120)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃⍴E←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.

APL (124 120 116)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄S↓'-',A/⍨∨\×A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1≠+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1≠+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be positive.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃⍴E←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • '-',A/⍨∨\×A: Remove any leading zeroes from the result vector, and add a minus in front.
  • S↓: Remove S characters from the front, i.e. if the result is supposed to be positive then remove the minus again.
fix explanation (said ∊ ipv ⍴E)
Source Link
marinus
  • 31.3k
  • 7
  • 73
  • 112

APL (124 120)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃∊←⌽Y∘ר⊃XL←⊃⍴E←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.

APL (124 120)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃∊←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.

APL (124 120)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃⍴E←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.
optimalisation
Source Link
marinus
  • 31.3k
  • 7
  • 73
  • 112

APL (124124 120)

This is way too long. It does grade school multiplication.

N←{⍵↑⍨¨-⌈/⍴¨⍵↓⌽↑⌽¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{⍵↑⍨¨-⌈/⍴¨⍵↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃∊←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.

APL (124)

This is way too long. It does grade school multiplication.

N←{⍵↑⍨¨-⌈/⍴¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{⍵↑⍨¨-⌈/⍴¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front.
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃∊←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.

APL (124 120)

This is way too long. It does grade school multiplication.

N←{↓⌽↑⌽¨⍵}⋄⎕←(S⍴'-'),,/A⊂⍨0≠A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}⊃+/N↑⍨⌿2L⍴E,(L-1)+⍳L←⊃⍴E←⌽Y∘ר⊃X Y←N⍎¨¨A~¨'-'⊣S←1=+/'-'∊¨A←⍞⍞ 

Explanation:

  • N←{↓⌽↑⌽¨⍵}: N is a function that, given a vector of vectors, makes all the vectors the same length, padding with zeroes at the front. It does this by reversing all inner vectors (⌽¨), turning the vector of vectors into a matrix (), reversing this matrix (), and turning it back into a vector of vectors ().
  • S←1=+/'-'∊¨A←⍞⍞: Read two lines of input, as characters (⍞⍞), store them in A, and count how many minuses there are in the input. S is zero or one depending on whether the result will be negative.
  • X Y←N⍎¨¨A~¨'-': In A (our input) without (~) the minuses, evaluate each separate character (⍎¨¨), run it through the N function and save the two vectors in X and Y. If the input was 123 32, we now have X=1 2 3 Y=0 3 2.
  • L←⊃∊←⌽Y∘ר⊃X Y: For each digit in the first vector, produce a vector with the digits from the second vector multiplied by that digit. Reverse this vector of vectors so that the least significant one is at the front. Store this vector in E and its length in L.
  • +/N↑⍨⌿2L⍴E,(L-1)+⍳L: Shift all of these vectors left, padding with zeroes on the right, by their index. Run them through N again so that they're all the same length. Then add all these vectors together. I.e.:
 1 2 3 3 2 ------ 2 4 6 (shift 0) 3 6 9 (shift 1) -------- 3 8 13 6 (before carry) 3 9 3 6 (after carry) 
  • A←{∨/M←9<T←0,⍵:∇(1⌽M)+T-M×10⋄⍵}: Carry the ones. As long as there are still 'digits' higher than 10 (∨/M←9<T←0,⍵), add one to the next significant digit (∇(1⌽M)+T) and remove 10 from the offending digit (-M×10).
  • ,/A⊂⍨0≠A: Remove any leading zeroes from the result vector.
  • ⎕←(S⍴'-'),: Output it, prefixed with a minus if the result was supposed to be negative.
Source Link
marinus
  • 31.3k
  • 7
  • 73
  • 112
Loading