32
2
People keep telling me that the square of a number is the number multiplied by itself. This is obviously false. The correct way to square a number is to make it into a square, by stacking it on top of itself a number of times equal to the number of digits it has, and then reading all the numbers from the resultant square, both horizontally (from left to right only) and vertically (from up to down only), and then adding them together. So, for the number 123, you first create the square:
123
123
123
Then you take all of the rows and columns from the square, and add them together:
123+123+123+111+222+333
Which gives us a result of 1035
.
For negative numbers, you stack normally (remember that you only count the number of digits, so the negative sign is not included in the length), and then read the horizontal numbers normally (with negative signs), and then ignore the negative signs for the vertical numbers. So, for the number -144
we get the square:
-144
-144
-144
Which gives us -144-144-144+111+444+444
, which equals 567
For numbers with only one digit, the square is always equal to the number doubled (read once horizontally and once vertically). So 4
gives us
4
Which gives us 4+4
, which equals 8
.
For numbers with decimal parts, stack normally (remember that only digits are counted in the number of times you stack the number, and therefore the decimal point is not counted), and ignore the decimal symbols when reading the vertical numbers. For example, the number 244.2
gives us
244.2
244.2
244.2
244.2
Which gives us 244.2+244.2+244.2+244.2+2222+4444+4444+2222
, which equals 14308.8
.
Fractional or complex numbers cannot be squared.
Your Task:
I'm tired of squaring numbers my way by hand, so I've decided to automate the process. Write me a program or function that takes a float or string, whichever you prefer, as input and returns the result of squaring it my way.
Examples:
123 -> 1035
388 -> 3273
9999 -> 79992
0 -> 0
8 -> 16
-6 -> 0
-25 -> 27
-144 -> 567
123.45 -> 167282.25
244.2 -> 14308.8
2 -> 4
-0.45 -> 997.65
0.45 -> 1000.35
Scoring:
My hands are getting cramped from writing out all those squares, and my computer doesn't support copy/paste, so the entry with the least amount of code for me to type (measured in bytes for some reason?) wins!
1"123.45" and "244.2" aren't valid floats in and of itself because the computer stores number in binary. This isn't normally a problem until the problem relies on the decimal representation. – Leaky Nun – 2017-07-29T13:22:20.143
@LeakyNun, I don't really know what you mean by that. The problem isn't unsolvable (at least in python), I'm pretty sure I could do it fairly easily, although in a large number of bytes. It would require some string manipulation, however. – Gryphon – 2017-07-29T13:27:25.117
@Gryphon So we must take input as a string? – Leaky Nun – 2017-07-29T13:28:08.560
No, but to solve this problem, you will probably have to convert the number to a string. – Gryphon – 2017-07-29T13:28:48.287
3@Gryphon This is where it fails.
244.2
is not a float number. It cannot be converted to the string"244.2"
. – Leaky Nun – 2017-07-29T13:29:10.207@LeakyNun, in python 2.7,
my_float = 244.2 print "My float is "+str(my_float)
seems to work just fine – Gryphon – 2017-07-29T13:31:09.517The space between "244.2" and "print" is supposed to be a newline. – Gryphon – 2017-07-29T13:31:46.677
3
@Gryphon But behaviours like this makes it very inconvenient.
– Leaky Nun – 2017-07-29T13:38:06.970