# Parallelogram a Number

Now that we know how to properly square and triangle a number, we are going to learn how to parallelogram one. To parallelogram a number, we first arrange it as a parallelogram by stacking it on top of itself a number of times equal to the number of digits it has, and adding spaces to make it a parallelogram. So 123 would form:

123
123
123

Now we take each horizontal and vertical number and add them, 123+123+123+1+12+123+23+3, which equals 531, which is the parallelogram of 123.

Write a program or function that, when given a number as input, returns the parallelogram of the number.

## Input:

A non-negative integer, or a non-negative integer represented by a string.

## Output:

The parallelogram of the integer.

## Test Cases:

1234567 -> 10288049
123     -> 531
101     -> 417
12      -> 39

## Scoring:

This is , lowest score in bytes wins!

• Sandbox Post – Gryphon Aug 8 '17 at 22:28
• Why the downvote? – Gryphon Aug 9 '17 at 16:14

# MATL, 12 bytes

tnEXyPY+c!Us

Input is a string. Try it online!

### Explanation

Consider input '123' as an example.

The code duplicates the input (t) and builds an identity matrix (Xy) of size twice the input length (nE):

1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1

then flips it upside down (P):

0 0 0 0 0 1
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 0
1 0 0 0 0 0

The input string, interpreted as ASCII codes of the digits, is equivalent to the numeric row vector

49 50 51

Full-size two-dimensional convolution (Y+) of the above vector and matrix gives

0  0  0  0  0 49 50 51
0  0  0  0 49 50 51  0
0  0  0 49 50 51  0  0
0  0 49 50 51  0  0  0
0 49 50 51  0  0  0  0
49 50 51  0  0  0  0  0

Interpreting those numbers back as ASCII codes (c) gives the following char matrix, with char 0 represented as space:

123
123
123
123
123
123

Transposition (!) transforms this into

1
12
123
123
123
123
23
3

Interpreting each row as a number (U) gives the numeric column vector

1
12
123
123
123
123
23
3

and summing it (s) gives the final result, 531.

• I smell... convolution – Adnan Aug 8 '17 at 23:18
• @Adnan What else? :-D – Luis Mendo Aug 8 '17 at 23:23

# Retina, 22 bytes

.
$;$&$';$_;
\d+
$* 1 Try it online! Link includes test cases. Explanation: The first stage splits the input number at each digit and includes all the exclusive prefixes and inclusive suffixes, giving the vertical numbers, plus also the original input number repeated for each digit, giving the horizontal numbers. The remaining stages then simply sum the resulting numbers. # 05AB1E, 12 11 8 bytes I'm sure knew this can could be golfed further - tips welcome! -1 byte thanks to Erik the Outgolfer (avoid wraps but using a concatenation) and then... -3 more bytes thanks to Adnan (avoid multiplication by length-1 by vectorising addition and subtracting the input off at the end) .s¹η++Oα Try it online! ### How? .s¹η++Oα - implicit input, say i e.g. 123 .s - suffixes [3,23,123] ¹ - push i 123 η - prefixes [1,12,123] + - addition of top two [4,35,246] + - addition (vectorises) [127,158,369] O - sum 654 α - absolute difference abs(123-654) 531 - implicit print • You can use « to concatenate the suffixes and the prefixes: g<*¹.s¹η«O+ – Erik the Outgolfer Aug 9 '17 at 9:48 • .s¹η++Oα should work for 8 bytes – Adnan Aug 9 '17 at 10:02 • Thanks @EriktheOutgolfer the two wraps did seem odd to me! – Jonathan Allan Aug 9 '17 at 10:15 • @Adnan - that's pretty sweet! – Jonathan Allan Aug 9 '17 at 10:16 • @JonathanAllan "tips welcome!" not sure if you'll get any anymore... – Erik the Outgolfer Aug 9 '17 at 10:16 # Husk, 13 12 bytes ṁit§+SRL§+ḣṫ Try it online! ### Explanation -- implicit input, say "123" §+ -- concatenate the results of the following two functions SR -- ¹repeat the input n times, where n is the result of the next function L -- length ["123","123"] §+ -- ²concatenate the results of the following two functions ḣ -- prefixes ["","1","12","123"] ṫ -- suffixes ["123","23","3",""] -- inner concatenation ["","1","13","123","123","23","3",""] -- outer concatenation ["123","123","","1","13","123","123","23","3",""] t -- all but the first element ["123","","1","13","123","123","23","3",""] ṁ -- map then sum i -- convert to integer (where "" converts to 0) # Haskell, 90787671646359 57 bytes g x=sum[x+div x(10^a)+mod x(10^a)|(a,_)<-zip[1..]$show x]

Try it online!

• g x=sum[x+div x a+mod x a|(a,_)<-zip((10^)<$>[1..])$show x]. – nimi Aug 9 '17 at 20:08
• g x=sum[x+div x(10^a)+mod x(10^a)|(a,_)<-zip[1..]$show x] is a hair shorter still. – Lynn Aug 9 '17 at 22:27 • g x=sum[x+xdiv10^a+xmod10^a|(a,_)<-zip[1..]$show x]. – Laikoni Aug 10 '17 at 12:19

# Neim, 7 bytes

𝐱S𝐗𝔻𝔻𝐬𝕤

Beware. Contains snakes: S𝐬𝕤

Try it online!

• "Beware. Contains snakes: S𝐬𝕤" Lol.. 𝐗𝔻 – Kevin Cruijssen Aug 10 '17 at 8:06

# Python 3, 85 70 Bytes

f=lambda n,r=1,i=int:n[r:]and i(n[r:])+i(n[:r])+f(n,r+1)+i(n)or i(n)*2

For input 12345:

Sums up slices of input 1+2345+12345, 12+345+12345, 123+45+12345, 1234+5+12345, by using string indexing to index (r) = 1,2,3,4 before casting to integer, and adds to 12345*2

Special Thanks to:

-14 Bytes @Jonathan Allen

-1 Byte @ovs

Try it online!

# Japt, 15 11 bytes

-4 bytes thanks to @Shaggy.

¬£iYç
cUz)x

Takes input as strings.

Try it online!

## Explanation

¬£

Split the input array to digits (¬) and map by (£) the following function, where Y is the index.
["1", "2", "3"]

iYç

The input value (implicit) with Y spaces (ç) inserted (i) at the beginning. This is assigned to U.
["123", " 123", " 123"]

cUz1)x

Concatenate that with itself rotated 90° right (1 time). Then sum (x).
["123", " 123", " 123", " 1", " 12", "123", "23 ", "1 "] -> 531.

• Exactly how I was trying to do it, but I couldn't quite get there, for some reason - nicely done :) Here's a 13 byte version. – Shaggy Aug 9 '17 at 10:23
• 11 bytes – Shaggy Aug 9 '17 at 10:29
• @Shaggy Awesome, I knew there had to be a shorter way to prepend the spaces to each line. Thanks! – Justin Mariner Aug 9 '17 at 10:32

# Japt, 31 18 bytes

-13 bytes thanks to @ETHproductions

This approach doesn't work well using Japt. Justin's solution is much better.

[U*Ål U¬£tYÃUå+]xx

## Explanation:

[U*Ål U¬£tYÃUå+]xx
[              ]    // Create a new array
U*Ål               // Push: Input * Input.slice(1).length()
// Push:
U¬            //   Input, split into chars
£tY         //   Map; At each char: .substr(Index)
Uå+     // Push: Cumulative reduce Input; with addition
xx  // Sum all the items, twice

Try it online!

• That's it, I'm adding a shortcut for Ul  :P – ETHproductions Aug 9 '17 at 1:36
• Hmm... you don't need either of the Us in the functions, and the middle item in the array can be condensed to Uå+ x, which I think gets you down to 23 bytes. – ETHproductions Aug 9 '17 at 1:53
• @ETHproductions Thanks! I got it down another byte by rearranging the array items. – Oliver Aug 9 '17 at 2:03
• Can you change mx x to xx? :-) – ETHproductions Aug 9 '17 at 2:07
• @ETHproductions I sure can, thanks again :) – Oliver Aug 9 '17 at 2:12

# Ruby, 61 55+1 = 56 bytes

Uses the -n flag. Input from STDIN.

p (1..~/$/).sum{|i|[$_[i,~/$/],$_[0,i],$_].sum &:to_i} Try it online! • For input 102033 your program prints 728714 while the correct value is 729702. – user72349 Aug 8 '17 at 22:58 • NOOOO! CURSE YOU OCTAL REPRESENTATIONS! (brb fixing, 02033 was the issue) – Value Ink Aug 8 '17 at 23:06 • I supposed that octal numbers are the problem, but I was not sure (btw I don't know ruby). Thanks for clarifying :) – user72349 Aug 8 '17 at 23:08 • @ThePirateBay no problem; I was already working on a shorter, alternate solution that took string inputs, and even with the fixes needed I actually ended up saving bytes anyways :) – Value Ink Aug 8 '17 at 23:13 # JavaScript, 77 74 bytes Saved 3 bytes thanks to Value Ink f=a=>[...a+a].map((_,b)=>a-=-z.substr((b-=n)>0?b:0,b+n),a*=n=(z=a).length)|a console.log(f('123')); console.log(f('101')); console.log(f('12')); console.log(f('1234567')); console.log(f('102033')); # Pyth, 20 19 bytes My current prefix approach (will hopefully golf further). +*tlQsQssM+_M.__Q._ # Explanation +*tlQsQssM+_M.__Q._ - Full program that reads a String from STDIN, with implicit input. tlQ - Length of the input - 1. sQ - The input converted to an integer. * - Product of the above two elements. We will call this P. ._ - Prefixes of the input. + - Concatenated with: _M.__Q - The prefixes of the reversed input, reversed. sM - Convert each to an integer. s - Sum. + - Addition of the product P and the sum above. To understand the concept better, we shall take an example, say "123". • We first get the prefixes of the input. Those are ['1', '12', '123']. • Then, we get the prefixes of the reversed input, i.e: ['3', '32', '321'] and reverse each, hence we get ['3', '23', '123']. • We concatenate the two lists and convert each element to an integer, so we obtain [3, 23, 123, 1, 12, 123]. • By summing the list, the result is 285. • The product P is the length of the input - 1 (i.e 2) multiplied by the integer representation of it (2 * 123 = 246). • In the end, we sum the two results: 285 + 246, hence we obtain 531, which is the correct result. # Pyth, 20 bytes +*hlQsQsm+s>Qds<QdtU Test Suite. # Explanation Explanation to come after further golfing. I didn't succeed to golf this further for now (I have ideas though). +*hlQsQsm+s>Qds<QdtUQ - Full program. Reads from STDIN. Q means input, and is implicit at the end. hlQ - Length of the input + 1. sQ - The input converted to an integer. * - Multiply the above. We'll call the result P. m tUQ - Map over [1...length of the input) s>Qd - input[currentItem:] casted to an integer. s<Qd - input[:currentItem] casted to an integer. + - Sum the above. s - Sum the list. + - Add the sum of the list and P. # q/kdb+, 34 bytes Solution: {sum"J"$((c#c),c-(!)2*c:(#)x)#\:x}

Examples:

q){sum"J"$((c#c),c-(!)2*c:(#)x)#\:x}"1234567" 10288049 q){sum"J"$((c#c),c-(!)2*c:(#)x)#\:x}"123"
531
q){sum"J"$((c#c),c-(!)2*c:(#)x)#\:x}"101" 417 q){sum"J"$((c#c),c-(!)2*c:(#)x)#\:x}"12"
39

Explanation:

{sum"J"$((c#c),c-til 2*c:count x)#\:x} / ungolfed { } / lambda function x / implicit input #\: / apply take (#) to each-left element with the right element ( ) / the left element c:count x / count length and save in variable c 2* / multiply by 2 (e.g. 6) til / range, so 0 1 2 3 4 5 c- / vector subtraction, so 3 2 1 0 -1 -2 ( ) / do this together c#c / 3 take 3, so 3 3 3 , / join, so 3 3 3 3 2 1 0 - 1 -2 "J"$                               / cast this "123", "123", "123" .. "23" to longs
sum                                   / sum them up and return result

Try it online!

# Mathematica, 77 bytes

(s=IntegerDigits@#;t=Length@s;Tr[FromDigits/@Table[s~Take~i,{i,-t,t}]]+t#-#)&