Skip to main content
Commonmark migration
Source Link

#JavaScript, 108 bytes r=[],i=a=b=0 while(a<=x)r.push(a=i++*i/2) for(a=0;a<3;){ b=r[i] if(b<=x){ x-=b a++ console.log(b)} else i--}

JavaScript, 108 bytes

r=[],i=a=b=0
while(a<=x)r.push(a=i++*i/2)
for(a=0;a<3;){
b=r[i]
if(b<=x){
x-=b
a++
console.log(b)}
else i--}

Explanation

###Explanation x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)

#JavaScript, 108 bytes r=[],i=a=b=0 while(a<=x)r.push(a=i++*i/2) for(a=0;a<3;){ b=r[i] if(b<=x){ x-=b a++ console.log(b)} else i--}

###Explanation x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)

JavaScript, 108 bytes

r=[],i=a=b=0
while(a<=x)r.push(a=i++*i/2)
for(a=0;a<3;){
b=r[i]
if(b<=x){
x-=b
a++
console.log(b)}
else i--}

Explanation

x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)

deleted 4 characters in body
Source Link
WaffleCohn
  • 311
  • 1
  • 7

#JavaScript, 112108 bytes var r=[]r=[],i=a=b=0 while(a<=x)r.push(a=i++*i/2) for(a=0;a<3;){ b=r[i] if(b<=x){ x-=b a++ console.log(b)} else i--}

###Explanation x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)

#JavaScript, 112 bytes var r=[],i=a=b=0 while(a<=x)r.push(a=i++*i/2) for(a=0;a<3;){ b=r[i] if(b<=x){ x-=b a++ console.log(b)} else i--}

###Explanation x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)

#JavaScript, 108 bytes r=[],i=a=b=0 while(a<=x)r.push(a=i++*i/2) for(a=0;a<3;){ b=r[i] if(b<=x){ x-=b a++ console.log(b)} else i--}

###Explanation x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)

Source Link
WaffleCohn
  • 311
  • 1
  • 7

#JavaScript, 112 bytes var r=[],i=a=b=0 while(a<=x)r.push(a=i++*i/2) for(a=0;a<3;){ b=r[i] if(b<=x){ x-=b a++ console.log(b)} else i--}

###Explanation x represents the input

while(a<=x)r.push(a=i++*i/2) Creates an array of all triangular numbers up to x

The for loop prints the highest triangular number less than x, then subtracts that number from x, for three iterations. (basically a greedy algorithm)