8
votes
8
votes
Calculate the sum of numbers in a rectangle
Python 3.8 (pre-release), 99 79 74 73 70 68 67 bytes
-20 bytes by removing the auxiliary t lambda, since it was only used once.
-5 bytes thanks to ...
8
votes
Calculate the sum of numbers in a rectangle
Python, 61 59 bytes
f=lambda a,b,c,d:c<d and((c+b)**3-b)/6+b*c-f(b,a,c+(b<a),d)
Attempt This Online!
Takes top incl, bottom excl, left incl, right excl.
How?
...
7
votes
Shift right by half a trit
Python 3, 55 bytes
f=lambda n,c=1:n>c and f(n,c*3)or[0,(n+c)//3,n-c][n//c]
Try it online!
The termination condition n>c ...
7
votes
Calculate the sum of numbers in a rectangle
Python 3, 75 bytes
Just wanted to see how far a closed formula can go. Uses the same inclusive bounds as in the question, in slightly different order.
...
6
votes
Shift right by half a trit
Jelly, 13 10 bytes
-3 bytes porting Dominic van Essen's implementation of my approach.
b3ḢḂȧ%3¬)S
Try it online! Or see the test-suite.
How?
...
6
votes
Calculate the sum of numbers in a rectangle
JavaScript (Node.js), 55 bytes
f=(x,y,X,Y)=>X?x<X&&f(y,x,Y)+f(x+1,y,X,Y):x-(y+=x)*~y/2
Try it online!
6
votes
Accepted
How many ways can you make change?
Jelly, 15 14 bytes
ŒṗịŒp€ẎṢ€Qɓ;Ṭ}
Try it online!
Oh yeah...
Given n on the left and the objects (with no 0 list!) on the right, outputs the nth collection of ...
5
votes
Shift right by half a trit
JavaScript (Node.js), 34 bytes
f=n=>n<2?0:!(n/3^f(1+f(--n)))+f(n)
Try it online!
Self is slow, but can save states to fasten
JavaScript (Node.js), 47 bytes
...
5
votes
Sum-of-four-squares grid
05AB1E, 50 49 bytes
∞Íonîü2íε`U0LsãεXô}Σ˜O]í€`.Δ[D˜g5‹#2δô2ô€øOOn}OOQ
Brute-force. Generates all \$2^k\times 2^k\$ sized matrices consisting of 0s and 1s, and will ...
5
votes
Sum-of-four-squares grid
Pyth, 48 bytes
M?Gho_SsNmsCMc2sgLtGdfqHs*VTT^UhH4]]Hgf>2eSsgTQZ
Try it online!
Can run up to about 30 before timing out online. Try a more performant, 5 byte ...
5
votes
How many ways can you make change?
JavaScript (V8), 80 bytes
Expects (list_of_lists)(n) without any list for weight 0. Outputs by printing comma-separated strings.
...
5
votes
How many ways can you make change?
Charcoal, 52 bytes
⊞υ⊞OEθκωFυ≡§I⁻↨E§ι±¹§θκ¹η⁰0…⮌ι¹-¿⊖Lι⊞⊞OυΦιλ⊞Oι⁺⊟ι§ι⁰
Attempt This Online! Link is to verbose version of code. Takes the collection as a ...
4
votes
Shift right by half a trit
Setanta, 65 bytes
gniomh(n){s:=1nuair-a s*3<=n s*=3toradh(n|0&n/s<2&(n+s)//3)|n-s}
try-setanta.ie link
Explanation
...
4
votes
Shift right by half a trit
Nekomata + -n, 10 bytes
R~3DᵉlhÖ*ž
Attempt This Online!
A port of @Dominic van Essen's implementation of @Jonathan Allan's ...
4
votes
Calculate the sum of numbers in a rectangle
Python 3, 73 bytes
lambda a,b,c,d:(e:=c-a)*(f:=d-b)*(e*e+f*f+3*(~a-b-c-d)**2-12*(a+c)-17)/24
Try it online!
I tried golfing a closed formula (independently of ovs)...
4
votes
How many ways can you make change?
05AB1E, 18 bytes
FĆ}¹Åœèε.«âεS{]€`Ù
Inputs in the order \$n,objectLists\$, including an empty list at index 0.
Assumes the objects are always single characters (...
3
votes
Rabinowitz-Wagon \$\pi\$ formula
R, 74 61 bytes
`~`=\(n,m=1)`if`(m>n,1,(a=n~m+1)[1]*(2*m+1)+m*a*0:1)*1:2^!m-1
Attempt This Online!
Recursive approach to construct the Rabinowitz-Wagon formula ...
3
votes
Factoriadic Fraction Addition
Wolfram Language (Mathematica), 71 bytes
If[#==0,{},{a=⌊++j#⌋,##&@@#0[j#-a]}]&@Tr[Tr[i=j=1;#/++i!&/@#]&/@#]&
Try it online!
Input ...
3
votes
3
votes
Cubic Concatenation
Excel, 23 Bytes
An anonymous workbook function that takes input from a vertical array at A1# and outputs to the calling cell
...
3
votes
Cubic Concatenation
ARBLE, 21 bytes
a^3+b^3+c^3-(a..b..c)
0 for truthy, non-zero for falsey.
Try it online!
Run all tests!
3
votes
Shift right by half a trit
R, 41 40 bytes
`?`=\(n)`if`(n<2,0,(k=?n-1)+!n%/%3-?1+k)
Attempt This Online!
Port of @l4m2's JS answer.
Slightly ungolfed:
...
3
votes
Shift right by half a trit
05AB1E, 15 bytes
_+3Bć©3αì®i¨}3ö
Try it online or verify all test cases.
Explanation:
...
3
votes
Shift right by half a trit
Nibbles, 18 nibbles (9 bytes)
,|,$~*%$3- 2/`@3
Attempt This Online!
An implementation of Jonathan Allan's approach of counting elements of A003605 up to ...
3
votes
Sum-of-four-squares grid
Charcoal, 101 bytes
Nθ≔⦃⁰⟦0⟧¹⟦1⟧⦄ηW∧¬§ηθη«≔⦃⦄η≔EιλζFζFζFζFζ«≔ΣX⟦νμλκ⟧²ε≔⁺E§ιν⁺ιμπE§ιλ⁺ικπδ¿∨¬§ηε‹ΣΣδΣΣ§ηε§≔ηεδ»»§ηθ
Attempt This Online! Link is to verbose ...
3
votes
Calculate the sum of numbers in a rectangle
Desmos, 49 bytes
f(a,b,c,d)=∑_{x=a}^b∑_{y=c}^d((x+y)^2+3x+y)/2
Order: inclusiveLeftX, inclusiveRightX, inclusiveTopY, inclusiveBottomY
Try it online
3
votes
Calculate the sum of numbers in a rectangle
Jelly, 11 9 bytes
-2 thanks to Unrelated String (use of the table hyper, þ, to avoid separately taking the Cartesian product with ...
3
votes
Calculate the sum of numbers in a rectangle
Haskell, 53 48 47 bytes
-5 bytes thanks to xnor
-1 byte thanks to Wheat Wizard ♦
f a b c d=sum$do i<-[a..c];j<-[b..d];j:[1..i+j]
Try it online!
The code ...
3
votes
How many ways can you make change?
Nekomata, 6 bytes
Ṗ@ᵐ~oũ
Attempt This Online!
A port of @Unrelated String's Jelly answer, but much shorter thanks to Nekomata's non-determinism.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
math × 1823code-golf × 1645
number × 303
sequence × 236
arithmetic × 185
number-theory × 183
geometry × 129
integer × 97
combinatorics × 89
decision-problem × 88
primes × 87
array × 77
code-challenge × 75
matrix × 71
string × 61
polynomials × 61
rational-numbers × 57
fastest-code × 51
abstract-algebra × 41
ascii-art × 39
base-conversion × 37
linear-algebra × 35
open-ended-function × 33
fibonacci × 30
restricted-source × 28