Ad
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)
    • const sum = arr => {
    • const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sum
    • return arr.reduce(reducer, 0);
    • }
    • 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)

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
    }
    • function calculate_multiples {
    • $SUM = 0
    • 1..999 | ForEach-Object {
    • if (!( $_ % 3) -OR !($_ % 5)) {
    • $Sum += $_
    • }
    • }
    • return $Sum
    • 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
    • }
Arrays
Data Types
Code
Diff
  • const remove=(arr, n)=>arr.filter((_,i)=>i!=n)
    • function remove (arr, n) {
    • if (n < 0 || n >= arr.length) return arr;
    • arr.splice(n, 1);
    • return arr;
    • }
    • const remove=(arr, n)=>arr.filter((_,i)=>i!=n)
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]
    }
    
    • 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
    • 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]
    • }
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
    • import java.util.*;
    • class Solution {
    • public static String largestNumber(Integer[] nums) {
    • Arrays.sort( nums, new Comparator<Integer>() {
    • @Override
    • public int compare(Integer a, Integer b) {
    • String aStr = a.toString();
    • String bStr = b.toString();
    • return (aStr + bStr).compareTo(bStr + aStr) * -1;
    • }
    • } );
    • String result = "";
    • for(Integer num : nums) {
    • result += num.toString();
    • }
    • return result;
    • }
    • }
    • 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