| Bytes | Lang | Time | Link |
|---|---|---|---|
| 053 | Uiua | 251009T145009Z | ojdo |
| 108 | C gcc | 251004T004341Z | c-- |
| 073 | R | 251003T202252Z | M-- |
| 111 | Swift 6 | 250425T151300Z | macOSist |
| 048 | Raku Perl 6 rakudo | 250425T142835Z | xrs |
| 072 | CASIO BASIC CASIO fx9750GIII | 250425T142114Z | madeforl |
| 091 | AWK | 250425T135233Z | xrs |
| 016 | Pip | 250326T225425Z | DLosc |
| 010 | Japt v2.0a0 x | 201109T121114Z | Shaggy |
| 881 | PHP | 170729T203855Z | Titus |
| 107 | Mathematica | 170809T215333Z | ZaMoC |
| 034 | Perl 5 | 170730T025315Z | Xcali |
| 062 | JavaScript | 170729T134355Z | user7234 |
| 137 | C# | 170731T122234Z | TheLetha |
| 133 | C# .NET Core | 170731T073850Z | Ian H. |
| 068 | Python 3 | 170731T132903Z | C McAvoy |
| 082 | Octave | 170729T185935Z | ბიმო |
| 134 | Swift 4 | 170730T061106Z | Mr. Xcod |
| 074 | Python 2 | 170729T155926Z | 0xffcour |
| 084 | Python 3 | 170729T135744Z | Mr. Xcod |
| 015 | Husk | 170729T190507Z | Zgarb |
| 023 | Brachylog | 170729T175633Z | Erik the |
| 056 | Haskell | 170729T145007Z | nimi |
| 020 | Pyth | 170729T144752Z | Mr. Xcod |
| 018 | Pyth | 170729T144730Z | Erik the |
| 016 | Japt v2 | 170729T144517Z | ETHprodu |
| 007 | 05AB1E | 170729T140901Z | Erik the |
| 012 | Jelly | 170729T140640Z | Jonathan |
| 017 | Jelly | 170729T132637Z | Erik the |
Uiua, 53 bytes
/+♭⬚0⋕⊂□⊙□⍉.⍥(.)-⊃(/+∊".-")⧻.
My first Uiua program, probably quite suboptimal. I hope I counted the bytes right (wc -c my-answer.uiua). Still lacking: correct and flexible rounding for the floating point numbers, though the result is "correct" numerically.
Breakdown
Takes input as a string on the stack.
. # Duplicate input, then
⊃ # both...
⧻ # ... count characters and..
(/+∊".-") # ... count [.-] occurrences
- # subtract both counts
⍥(.) # repeat input that often into an array
⍉. # transpose a copy of that array
□⊙□ # "box" both arrays
⊂ # and join them
⬚0⋕ # convert strings to numbers, default 0
♭ # flatten the result to 1D
/+ # sum-reduce
C (gcc), 108 bytes
c,e;f(char*s,double*r){double atof(),d=atof(s),f=*r=0;for(e=1;c=*s++;)c>47?e*=10,f+=c-48,*r+=d:0;*r+=e/9*f;}
R, 79 73 bytes
-6 bytes by Giuseppe
\(n,s=el(strsplit(c(n,""),"")),`~`=sum)strtoi(strrep(s[z<-s>"/"],~z))~z*n
Swift 6, 170 111 bytes
{s in{d in.init(d.count)*Float(s)!+d.reduce(0){$0+Float({String.init}()($1,d.count))!}}((s+"").filter{$0>"/"})}
Raku (Perl 6) (rakudo), 48 bytes
{$_*($!=(my@z=.comb(/\d/)))+[+] @z.map:{$_ x$!}}
{$_* # input number multiplied by
($! = # number of digits
(my@z= # create array
.comb(/\d/))) # of only digits
+ # add to it
[+] # coerce int from string
@z.map: # each element of array
{$_ x$!} # string of chars times number of digits
} # implicit return
CASIO BASIC (CASIO fx-9750GIII), 72 bytes
?→Str 1
StrLen(Str 1)→E
Exp(Str 1)E→A
For 1→I To E
For 0→J To E-1
A+₁₀J(Exp(StrMid(Str 1,I,1))→A
Next
Next
yerp
AWK, 91 bytes
{for(x=$0*(y=length-gsub(/-|\./,X));i++<split($0,a,X);x+=g)for(g=j=0;j++<y;)g=g a[i]}1,$0=x
{ # implicit input
for( # main loop
x= # running total
$0*( # input string multiplied by
y= # length of just digits
length- # length of input -
gsub( # returns subs count
/-|\./, # sub - and .
X)); # deletes - and .
i++< # for each char
split($0,a,X); # split new number into digits
x+=g) # add new column
for( # append loop
g=j=0; # reset
j++<y;) # append times length of digits
g=g a[i] # append char (1 1 -> 11)
}1, # print even if zero
$0=x # set default output
Pip, 16 bytes
YaMa@XD$+:yALZJy
Explanation
YaMa@XD$+:yALZJy
a ; Input number
@XD ; Regex match all digit characters
aM ; In that list, replace each value with the input number
Y ; Store the resulting list in y
ZJy ; Transpose y, joining rows back into strings/numbers
yAL ; Append that list to y
$+: ; Sum
Strings like --- or .... that are not numbers are treated as 0 in numeric contexts.
Japt v2.0a0 -x, 10 bytes
Takes input as a string.
f\d çU
cUy
f\d çU\ncUy :Implicit input of string U > "-0.45"
f :Match (returns an array)
\d : RegEx /\d/g > ["0","4","5"]
çU :Fill with U > ["-0.45","-0.45","-0.45"]
\n :Reassign to U
c :Concatenate
Uy : U transposed > ["-0.45","-0.45","-0.45","---","000","...","444","555"]
:Implicit output of sum of resulting array > 997.65
PHP, 78 88 +1 bytes
for($e=preg_match_all("#\d#",$n=$argn);~$c=$n[$i++];)$s+=str_repeat($c,$e);echo$s+$n*$e;
Run as pipe with -nR.
May yield warnings in PHP 7.1. Repace $c,$e with $c>0?$c:0,$e to fix.
Mathematica, 107 bytes
(t=Length[s=#&@@RealDigits[#]//.{a___, 0}:>{a}];If[IntegerPart@#==0,t++];t#+Tr[FromDigits@Table[#,t]&/@s])&
Perl 5, 37 33 + 1 (-p) = 38 34 bytes
$_*=@n=/\d/g;for$\(@n){$_+=$\x@n}
Used some tricks from Dom's code to shave 4 bytes
Explained:
@n=/\d/g; # extract digits from input
$_*=@n; # multiply input by number of digits
for$\(@n){ # for each digit:
$_+= # add to the input
$\x@n} # this digit, repeated as many times as there were digits
# taking advantage of Perl's ability to switch between strings
# and numbers at any point
JavaScript, 75 62 bytes
a=>(b=a.match(/\d/g)).map(b=>a+=+b.repeat(c),a*=c=b.length)&&a
-2 bytes thanks to Arnauld
-5 bytes thanks to Shaggy (I though the function must receive a number, but now I see that lot of other answers receive string too)
C#, 139 137 bytes
using System.Linq;n=>{var d=(n+"").Where(char.IsDigit);return d.Sum(i=>int.Parse(new string(i,d.Count())))+new int[d.Count()].Sum(_=>n);}
Saved 2 bytes thanks to @Ian H.
Full/Formatted version:
namespace System.Linq
{
class P
{
static void Main()
{
Func<double, double> f = n =>
{
var d = (n + "").Where(char.IsDigit);
return d.Sum(i => int.Parse(new string(i, d.Count()))) + new int[d.Count()].Sum(_ => n);
};
Console.WriteLine(f(123));
Console.WriteLine(f(-144));
Console.WriteLine(f(4));
Console.WriteLine(f(244.2));
Console.ReadLine();
}
}
}
C# (.NET Core), 150 141 133 bytes
Saved 9 bytes thanks to @TheLethalCoder
Saved another 8 bytes thanks to @TheLethalCoder
a=>{var c=(a+"").Replace(".","").Replace("-","");int i=0,l=c.Length;var r=a*l;for(;i<l;)r+=int.Parse(new string(c[i++],l));return r;}
Takes a string as an input and outputs the 'squared' number as a float.
This code follows the following algorithm:
Create a new string from the input, but without the decimal points and symbols, so we can get our length and the numbers for the columns from there.
Calculate the input times the length of the string we created at point 1.
For each column in our 'square', create a new string with the column number and the row length and add it to our result.
Example:
Input: -135.5
- If we replace decimal points and symbols we get the string
1355, which has a length of4. - The input times 4:
-135.5 * 4 = -542. - Now we create new strings for each column, parse them and add them to our result:
1111,3333,5555,5555.
If we sum these numbers up we get 15012, which is exactly what our program will output.
Python 3, 68 70 73 77 bytes
lambda n:sum(float(n)+int(_*sum(x>"/"for x in n))for _ in n if"/"<_)
Loops over every digit character and repeats it by the number of digit characters overall, makes that into an integer, and adds that to n. This way n gets added d times, the horizontal part of the sum, along with the digit repetition, which is the vertical part. Originally used str.isdigit but >"/", thanks to others in this thread, saved a lot of bytes. Saves two bytes by taking n as a string, but the output is messier.
lambda n:sum(n+int(_*sum(x>"/"for x in str(n)))for _ in str(n)if"/"<_)
Octave, 100 82 bytes
Thanks a lot @TomCarpenter for teaching me that assignments have a return value and saving me 18 bytes!
@(v)(n=nnz(s=strrep(num2str(abs(v)),'.','')-'0'))*v+sum(sum(s'*logspace(0,n-1,n)))
Ungolfed/Explanation
function f=g(v)
s=strrep(num2str(abs(v)),'.','')-'0'; % number to vector of digits (ignore . and -)
n=nnz(s); % length of that vector
f=n*v+sum(sum(s'*logspace(0,n-1,n))) % add the number n times and sum the columns of the square
end
The way this works is that we basically need to add the number itself n times and then add the sum of the columns. Summing s' * logspace(0,n-1,n) achieves the sum of columns, for example if v=-123.4 that matrix will be:
[ 1 10 100 1000;
2 20 200 2000;
3 30 300 3000;
4 40 400 4000 ]
So we just need to sum it up and we're done.
Swift 4, 139 134 bytes
func f(s:String){let k=s.filter{"/"<$0};print(Float(s)!*Float(k.count)+k.map{Float(String(repeating:$0,count:k.count))!}.reduce(0,+))}
Explanation
func f(s:String)- Defines a functionfwith an explicit String parameters.let k=s.filter{"/"<$0}- Filters the digits: I noticed that both-and.have smaller ASCII-values than all the digits, and/is between.,-and0. Hence, I just checked if"/"is smaller than the current character, as I did in my Python answer.print(...)- Prints the result.Float(s)!*Float(k.count)- Converts both the String and the number of digits to Float and multiplies them (Swift does not allow Float and Int multiplication :()). So it adds the numberxtimes, wherexis the number of digits it contains.k.map{Int(String(repeating:$0,count:k.count))!-k.map{}maps overkwith the current value$0.String(repeating:$0,count:k.count)takes each digit, creates a String ofxidentical digits andFloat(...)!converts it to a Floating-point number..reduce(0,+)- Gets the sum of the list above.And finally
+sums the two results.
Let's take an example!
Say our String is "0.45". First off, we filter the digits, so we are left with 0, 4, 5. We convert "0.45" to Float and multiply by the number of digits: 0.45 * 3 = 1.35. Then we take each digit and turn it into a String repeating that digit until it fills the width of the square (how many digits there are): 0, 4, 5 -> 000, 444, 555. We sum this, 000 + 444 + 555 = 999. Then we just add the results together: 1.35 + 999 = 1000.35.
Python 2, 81 74 bytes
-7 bytes thanks to @Mr. Xcoder: '/'<i
- Takes in integer or float, returns float.
lambda x:sum(float(i*len(z))for z in[[i for i in`x`if"/"<i]]for i in[x]+z)
Explanation:
Say 123.45 is given as input. [i for i in`x`if"/"<x] gives a list of stringified integers ['1','2','3','4','5'] (which is also z). Now we iterate through [x]+z i.e. [123.45,'1','2','3','4','5'], multiplying each element by len(z), here 5 and converting each to a Float (so that strings also convert accordingly), yielding [617.25,11111.0,22222.0,33333.0,44444.0,55555.0]. Finally we calculate the sum(...) and obtain 167282.25.
Python 3, 95 94 87 85 84 bytes
def f(i):l=[x for x in i if"/"<x];k=len(l);print(k*float(i)+sum(int(x*k)for x in l))
Python 3, 78 bytes
lambda x:sum(float(i*len(z))for z in[[i for i in str(x)if"/"<i]]for i in[x]+z)
The second approach is a port to Python 3 inspired by @officialaimm's solution.
Husk, 15 bytes
§+ȯṁrfΛ±TṁrSR#±
Takes a string and returns a number. Try it online!
Explanation
It's a bit annoying that the built-in parsing function r gives parse errors on invalid inputs instead of returning a default value, which means that I have to explicitly filter out the columns that consist of non-digits.
If it returned 0 on malformed inputs, I could drop fΛ± and save 3 bytes.
§+ȯṁrfΛ±TṁrSR#± Implicit input, e.g. "-23"
#± Count of digits: 2
SR Repeat that many times: ["-23","-23"]
ṁr Read each row (parse as number) and take sum of results: -46
ȯṁrfΛ±T This part is also applied to the result of SR.
T Transpose: ["--","22","33"]
fΛ± Keep the rows that contain only digits: ["22","33"]
ṁr Parse each row as number and take sum: 55
§+ Add the two sums: 9
Brachylog, 23 bytes
{∋ịṫ}ᶠ⟨≡zl⟩j₎ᵐ;[?]zcịᵐ+
Brachylog doesn't go well with floats...
Explanation:
{∋ịṫ}ᶠ⟨≡zl⟩j₎ᵐ;[?]zcịᵐ+ Takes string (quoted) input, with '-' for the negative sign
ᶠ Return all outputs (digit filter)
{ } Predicate (is digit?)
∋ An element of ? (input)
ị Convert to number (fails if '-' or '.')
ṫ Convert back to string (needed later on)
⟨ ⟩ Fork
≡ Identity
l Length
with
z Zip
ᵐ Map
₎ Subscript (optional argument)
j Juxtapose (repeat) (this is where we need strings)
; Pair with literal
[ ] List
? ?
z Zip
c Concatenate (concatenate elements)
ᵐ Map
ị Convert to number
+ Add (sum elements)
Haskell, 59 56 bytes
f s|l<-filter(>'.')s=0.0+sum(read<$>(s<$l)++[c<$l|c<-l])
The input is taken as a string.
How it works
l<-filter(>'.')s -- let l be the string of all the numbers of the input string
f s = 0.0 + sum -- the result is the sum of (add 0.0 to fix the type to float)
read<$> -- turn every string of the following list into a number
s<$l -- length of l times the input string followed by
[c<$l|c<-l] -- length of l times c for each c in l
Pyth, 21 20 bytes
K@jkUTQ+smv*lKdK*lKv
Uses a completely different approach from @EriktheOutgolfer's answer, which helped me golf 1 byte in chat, from 22 to 21.
Explanation
K@jkUTQ+s.ev*lKbK*lKv
K@jkUTQ - Filters the digits and assigns them to a variable K.
m - Map. Iterated through the digits with a variable d
v - Evaluate (convert to float).
*lKd - Multiplies each String digit by the length of K.
s - Sum
+ - Sum
*lKvQ - Multipies the number by the length of the digits String
Japt v2, 16 bytes
o\d
l
¬xpV +V*Ng
Explanation
o\d First line: Set U to the result.
o Keep only the chars in the input that are
\d digits. (literally /\d/g)
l Second line: Set V to the result.
l U.length
¬xpV +V*Ng Last line: implicitly output the result.
¬ Split U into chars.
x Sum after
pV repeating each V times.
+V*Ng Add V * first input (the sum of the horizontals) to the result.
05AB1E, 7 bytes
þSDg×+O
Explanation
þSDg×+O Implicit input
þ Keep digits
S Get chars
D Duplicate
g Length
× Repeat string(s)
+ Add (implicit input added to all elements)
O Sum
Jelly, 13 12 bytes
fØDẋ€L$ŒV+VS
A monadic link accepting a list of characters (a well-formed decimal number, the single leading zero being a requirement for -1 < n < 1) and returning a number.
14 bytes to accept and return numbers (input limited at +/-10-5 by ŒṘ): ŒṘfØDẋ€L$ŒV+⁸S.
How?
fØDẋ€L$ŒV+VS - Link: list of characters e.g. "-0.45"
ØD - yield digit characters "0123456789"
f - filter keep "045"
$ - last two links as a monad:
L - length (number of digit characters) 3
ẋ€ - repeat list for €ach digit ["000","444","555"]
ŒV - evaluate as Python code (vectorises) [0,444,555]
V - evaluate (the input) as Jelly code -0.45
+ - addition (vectorises) [-0.45,443.55,554.55]
S - sum 997.65