### Sum array elements

Code
Diff
• ``````const plus= (a,b,p) => a%1||b%1?plus(+(a+"0").replace(/(\.)(\d)/,"\$2\$1"),+(b+"0").replace(/(\.)(\d)/,"\$2\$1"),p+1):(a+b)/Math.pow(10,p)
const sum = arr => arr.reduce((a,b)=>0+b===b?plus(a,b,0):a,0)``````
•  1 − ```const sum = arr => { ``` 2 − ``` const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sum ``` 3 − ``` return arr.reduce(reducer, 0); ``` 4 − ```} ``` 5 − 1 + ```const plus= (a,b,p) => a%1||b%1?plus(+(a+"0").replace(/(\.)(\d)/,"\$2\$1"),+(b+"0").replace(/(\.)(\d)/,"\$2\$1"),p+1):(a+b)/Math.pow(10,p) ``` 2 + ```const sum = arr => arr.reduce((a,b)=>0+b===b?plus(a,b,0):a,0) ```

### Project Euler: Problem 1X

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Your task is to find the sum of all the multiples of 3 or 5 below `n`.

Code
Diff
• ``````function sumOfMultiplesUnder(n) {
return sumAll(--n,3)+sumAll(n,5)-sumAll(n,15)
}
function sumAll(n,m){
return m*(Math.floor(n/m)+1)*Math.floor(n/m)/2
}``````
•  1 − ```function calculate_multiples { ``` 2 − 3 − ``` \$SUM = 0 ``` 4 − 5 − ``` 1..999 | ForEach-Object { ``` 6 − ``` if (!( \$_ % 3) -OR !(\$_ % 5)) { ``` 7 − ``` \$Sum += \$_ ``` 8 − ``` } ``` 9 − ``` } ``` 10 − 11 − ``` return \$Sum ``` 1 + ```function sumOfMultiplesUnder(n) { ``` 2 + ``` return sumAll(--n,3)+sumAll(n,5)-sumAll(n,15) ``` 3 + ```} ``` 4 + ```function sumAll(n,m){ ``` 5 + ``` return m*(Math.floor(n/m)+1)*Math.floor(n/m)/2 ``` 12 12 ```} ```

### Remove Element from Array

Arrays
Code
Diff
• ``const remove=(arr, n)=>arr.filter((_,i)=>i!=n)``
•  1 − ```function remove (arr, n) { ``` 2 − ``` if (n < 0 || n >= arr.length) return arr; ``` 3 − ``` arr.splice(n, 1); ``` 4 − ``` return arr; ``` 5 − ```} ``` 1 + ```const remove=(arr, n)=>arr.filter((_,i)=>i!=n) ```

### Return Largest Possible Number

Code
Diff
• ``````function solution (nums){
if (nums.length<2) return nums.join("")
var lens=nums.map(x=>(x+"").length).sort((a,b)=>b-a),maxlen=lens[0]+lens[1],zeros="0".repeat(maxlen),
firsttwo=nums.reduce((a,b,i)=>a.concat(nums.map((c,j)=>i==j?[0,0,"0"]:[b,c,(""+b+c+zeros).slice(0,maxlen)])),[]).sort((a,b)=>b[2]-a[2]).filter((x,i,ar)=>x[2]==ar[0][2])
return firsttwo.map(x=>""+x[0]+x[1]+solution(nums.filter((y,i)=>i!=nums.indexOf(x[0])&&i!=nums.indexOf(x[1])))).sort((a,b)=>b-a)[0]
}
``````
•  1 − ```def solution (nums) ``` 2 − ``` # slower but simple ;-) ``` 3 − ``` nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0] ``` 4 − ```end ``` 1 + ```function solution (nums){ ``` 2 + ``` if (nums.length<2) return nums.join("") ``` 3 + ``` var lens=nums.map(x=>(x+"").length).sort((a,b)=>b-a),maxlen=lens[0]+lens[1],zeros="0".repeat(maxlen), ``` 4 + ``` firsttwo=nums.reduce((a,b,i)=>a.concat(nums.map((c,j)=>i==j?[0,0,"0"]:[b,c,(""+b+c+zeros).slice(0,maxlen)])),[]).sort((a,b)=>b[2]-a[2]).filter((x,i,ar)=>x[2]==ar[0][2]) ``` 5 + ``` return firsttwo.map(x=>""+x[0]+x[1]+solution(nums.filter((y,i)=>i!=nums.indexOf(x[0])&&i!=nums.indexOf(x[1])))).sort((a,b)=>b-a)[0] ``` 6 + ```} ```

### Return Largest Possible Number

Code
Diff
• ``````def solution (nums)
# slower but simple ;-)
nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0]
end``````
•  1 − ```import java.util.*; ``` 2 − 3 − ```class Solution { ``` 4 − ``` public static String largestNumber(Integer[] nums) { ``` 5 − ``` Arrays.sort( nums, new Comparator() { ``` 6 − ``` ``` 7 − ``` @Override ``` 8 − ``` public int compare(Integer a, Integer b) { ``` 9 − ``` String aStr = a.toString(); ``` 10 − ``` String bStr = b.toString(); ``` 11 − ``` ``` 12 − ``` return (aStr + bStr).compareTo(bStr + aStr) * -1; ``` 13 − ``` } ``` 14 − ``` ``` 15 − ``` } ); ``` 16 − ``` ``` 17 − ``` String result = ""; ``` 18 − ``` for(Integer num : nums) { ``` 19 − ``` result += num.toString(); ``` 20 − ``` } ``` 21 − ``` ``` 22 − ``` return result; ``` 23 − ``` } ``` 24 − ```} ``` 1 + ```def solution (nums) ``` 2 + ``` # slower but simple ;-) ``` 3 + ``` nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0] ``` 4 + ```end ```