| Bytes | Lang | Time | Link |
|---|---|---|---|
| 305 | Wolfram Language Mathematica | 240802T110622Z | 138 Aspe |
| 071 | Wolfram Language Mathematica | 240815T132148Z | att |
| 016 | 05AB1E legacy | 240812T074436Z | Kevin Cr |
| 146 | Setanta | 240802T124842Z | bb94 |
| 125 | R | 240727T110821Z | Dominic |
| 166 | JavaScript Node.js | 240727T010056Z | Andrew B |
| 017 | Jelly | 240726T201505Z | Jonathan |
| 018 | Nekomata + 1 | 240726T091817Z | alephalp |
| 057 | Charcoal | 240726T083053Z | Neil |
| 093 | JavaScript Node.js | 240726T064144Z | l4m2 |
| 035 | APLDyalog Unicode | 240726T081812Z | ovs |
Wolfram Language (Mathematica), 305 bytes
Edit:
87 bytes version provided by @att, doing exactly the same thing.
Total@PadRight@#//.{a_,b___,0...}:>{a}~Join~Most[e=Mod[c={b,0},i=2;d=++i&/@c]]+(c-e)/d&
Original answer:
A port of @Dominic van Essen' R code in Mathematica.
Golfed version. Try it online!
f[x_,y_]:=Module[{},a=ConstantArray[0,Max[Length@x,Length@y]+1];a[[;;Length@x]]=x;a[[;;Length@y]]+=y;While[1<2,b=Table[Boole[a[[i]]>i],{i,Length@a}];t=a;t=t+RotateLeft[b,1]-b*Table[i+1,{i,Length@a}];If[!AnyTrue[MapThread[#-#2&,{a,t}],#!=0&],Break[]];a=t];a[[;;Length[Select[Range[Length@a],a[[#]]!=0&]]]]]
Ungolfed version. Try it online!
Clear["Global`*"];
addFactoriadic[x_List, y_List] :=
Module[{maxLength, a, b, tempA, finalResult},
maxLength = Max[Length[x], Length[y]];
a = ConstantArray[0, maxLength + 1];
a[[;; Length[x]]] = x;
a[[;; Length[y]]] += y;
While[True, b = Table[Boole[a[[i]] > i], {i, Length[a]}];
tempA = a;
tempA = tempA + RotateLeft[b, 1] - b*Table[i + 1, {i, Length[a]}];
If[! AnyTrue[MapThread[# - #2 &, {a, tempA}], # != 0 &], Break[]];
a = tempA;];
finalResult =
a[[;; Length[Select[Range[Length[a]], a[[#]] != 0 &]]]];
finalResult]
(*Test cases*)
addFactoriadic[{}, {}] (*{}*) //Print
addFactoriadic[{}, {1}] (*{1}*) //Print
addFactoriadic[{0, 2}, {-1}] (*{-1,2}*) //Print
addFactoriadic[{0, 2}, {0, 2}] (*{1,1}*) //Print
addFactoriadic[{0, 1, 2}, {0, 1, 2}] (*{1}*) //Print
Wolfram Language (Mathematica), 71 bytes
If[#==0,{},{a=⌊++j#⌋,##&@@#0[j#-a]}]&@Tr[Tr[i=j=1;#/++i!&/@#]&/@#]&
Input [{numbers...}].
Tr[i= 1;#/++i!&/@#]&/@# from factoriadic
Tr[ ] sum
If[#==0,{},{a=⌊++j#⌋,##&@@#0[j#-a]}]&@ j=1 to factoriadic
05AB1E (legacy), 16 bytes
ζOāvćsā̉‚˜2ôO0Ü
First halve of the program is a port of @alephalpha's Nekomata's answer and the second halve is a port of @JonathanAllan's Jelly answer, so make sure to upvote both those answers as well!
Input as a pair of lists.
Try it online or verify all test cases.
Explanation:
Uses the legacy version of 05AB1E so it won't need an additional leading 0, since invalid characters in a list when summing (a space in this case) are simply ignored in the legacy version.
ζ # Zip/transpose the (implicit) input-pair; swapping rows/columns,
# using an implicit space " " as filler for unequal length rows
O # Sum each inner list, ignoring the spaces
ā # Push a list in the range [1,length] (without popping)
v # Pop and loop over those items (without using them),
# aka, `āv` is used to loop the length amount of times:
ć # Extract head; push first item and remainder-list separately
s # Swap so the remainder-list is at the top of the stack
ā # Push a list in the range [1,length] (without popping)
Ì # Increase each by 2 to make the range [3,length+2]
‰ # Divmod the remainder-list by this [3,length+2]-list
‚ # Pair it with the extracted head
˜ # Flatten this list
2ô # Split it into pairs
O # Sum each pair
0Ü # Trim any trailing 0s
# (after the loop, the resulting list is output implicitly)
Setanta, 175 153 150 146 bytes
gniomh(x,y){z:=x+[0]*(fad@y-fad@x)le i idir(0,fad@y)z[i]+=y[i]le i idir(fad@z-1,0)ma i+1{z[i-1]+=z[i]//(i+2)z[i]%=i+2z[i]|scrios_cul@z()}toradh z}
R, 125 bytes
\(v,w,`^`=c,y=w^0,s=seq){a=(!s(x<-v^0)-(e=s(x+y)))*x+y*!s(y)-e
while(any(a-(a=a-(b=(a>e))*e-b+b[-1]^0)))1
a[s(l=max(e*!!a))]}
How? - ungolfed version:
add_factoriadic=function(x,y){
a<-c(x,y,0)&0 # define a as long-enough vector of zeros
a[seq(!x)]=x # add x to appropriate elements
a[seq(!y)]=a[seq(!y)]+y # add y to appropriate elements
while({ # repeat while value of a changes:
b=(a>seq(a)) # b = carry digits
any(a- # check whether a changes by
(a=a+c(b[-1],0) # add carry digits at position to left
-b*(seq(a)+1))) # and subtract carried values
})1 # (do nothing else)
a[seq(length=max(seq(a)*!!a))] # finally, output a without trailing zeros
}
JavaScript (Node.js), 166 bytes
(a,b)=>{b.map((x,i)=>a[i]=(a[i]?a[i]:0)+x);c=0;for(i=(y=a.length)-1;i>0;i--){d=(e=a[i]+c)%(z=i+2);c=e/z|0;a[i]=d};y?a[0]+=c:0;while(a[a.length-1]==0)a.pop();return a}
Expanded, with comments:
(a,b)=>{
b.map((x,i)=>a[i]=(a[i]?a[i]:0)+x); //add elements in arrays a and b,
//allowing for different size arrays.
c=0; //initialize carry
for(i=(y=a.length)-1;i>0;i--){ //adjust each value so it fits within the max
//for that digit,
//carrying to the left as required.
d=(e=a[i]+c)%(z=i+2); //use mod for the digit
c=e/z|0; //use divide and round for the carry
a[i]=d //put the digit back in the array
};
y?a[0]+=c:0; //add remaining carry to the first digit
while(a[a.length-1]==0)a.pop(); //remove trailing zeros
return a //return result
}
Jelly, 17 bytes
+dṛ¦J‘$F+2/ƲÐLœr0
A dyadic Link that accepts factoriadic fractions as lists of integers on each side and yields their sum.
Try it online! Or see the test-suite.
How?
Repeated carrying...
+dṛ¦J‘$F+2/ƲÐLœr0 - Link: integer list, [a,b,c,...]; integer list, [x,y,z,...]
+ - {A} add {B} (vectorises) -> S = [a+x, b+y, c+z, ...]
ÐL - repeat until the results are no longer unique:
Ʋ - last four links as a monad:
$ - last two links as a monad:
J - indices -> [1,2,...N]
‘ - increment -> I = [2,3,...,N+1]
¦ - sparse application (to {V in S} with {i in I})...
ṛ - ...to indices: right argument = I
i.e. apply to all but the first element
d - ...action: {V} divmod {i} -> [V//i, V%i]
F - flatten -> [a+x, (b+y)//3, (b+y)%3, (c+z)//4, (c+z)%4, ...]
2/ - 2-wise reduce by:
+ - addition -> [a+x+(b+y)//3, (b+y)%3+(c+z)//4, ...]
œr0 - trim trailing zeros
Nekomata + -1, 18 bytes
+:#ᵑ{CU$x3+þç++;ž¿
+:#ᵑ{CU$x3+þç++;ž¿
+ Add two lists; pad with 0s
e.g. [0,2] [-1] -> [-1,2]
: Duplicate
# Length
ᵑ{ Repeat the following function that many times:
CU$ Split into the head and the rest
e.g. [0,2,4] -> [0] [2,4]
x3+ Push [3,...,length+2]
e.g. [0] [2,4] -> [0] [2,4] [3,4]
þ Divmod; get the quotient and the remainder
e.g. [0] [2,4] [3,4] -> [0] [0,1] [2,0]
ç Prepend 0 to the remainder
e.g. [0] [0,1] [2,0] -> [0] [0,1] [0,2,0]
++ Add the three lists
e.g. [0] [0,1] [0,2,0] -> [0,3,0]
;ž¿ Remove some trailing 0s
e.g. [0,3,0] -> [0,3]
The last step ;ž¿ is nondeterministic, so we need to add the -1 flag to return only the first result, where all trailing 0s are removed.
Charcoal, 57 bytes
F⁻LηLθ⊞θ⁰≔⁰ζF⮌Eηκ«≧⁺⁺§θι⊟ηζ§≔θι⎇ι﹪ζ⁺²ιζ≧÷⁺²ιζ»W∧θ¬↨θ⁰⊟θIθ
Try it online! Link is to verbose version of code. Explanation:
F⁻LηLθ⊞θ⁰
Extend the first input to the length of the second.
≔⁰ζ
Start with no carry.
F⮌Eηκ«
Enumerate the indices of the second array in reverse.
≧⁺⁺§θι⊟ηζ
Add the values in the two array elements to the carry.
§≔θι⎇ι﹪ζ⁺²ιζ
Update the output value, reducing modularly if necessary.
≧÷⁺²ιζ
Update the carry.
»W∧θ¬↨θ⁰⊟θ
Remove any trailing zeros.
Iθ
Output the final result.
Bonus 136-byte Retina 0.8.2 solution: Try it online! Does not work with negative numbers.
JavaScript (Node.js), 93 bytes
f=([a=0,...A],[b=c=0,...B],i)=>k=A+B||a|b?f(A,B,i+1||2)!=-(b+=c+a,c=b>i,b+=c*~i)?[b,...k]:k:A
==- if former is [] and latter is 0. Former is never negative and latter, including -, is never positive
APL(Dyalog Unicode), 35 bytes SBCS
Assumes index origin 0.
{x↓⍨-⊥⍨0=x←(⊢⊤⊥∘m)(⊢+2××)⍳≢m←+⌿↑⍺⍵}
Add the inputs element-wise, convert back and forth using mixed base 0 3 4 5 ..., drop trailing zeros.