# Find the sum of all numbers below n that are a multiple of some set of numbers

Almost equivalent to Project Euler's first question:

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.

Find the sum of all the multiples of 3 or 5 below 1000.

## Challenge:

Given a positive integer N and a set of at least one positive integer A, output the sum of all positive integers less than N that are multiples of at least one member of A.

For example, for the Project Euler case, the input would be:

1000
3
5


## Test cases:

Input : 50, 
Output: 600

Input : 10, [3, 5]
Output: 23

Input : 28, [4, 2]
Output: 182

Input : 19, [7, 5]
Output: 51

Input : 50, [2, 3, 5]
Output: 857

• 1) Do we count numbers that are multiples of both twice? 2) Can we only get two other numbers? or any amount say one or 3? – Ad Hoc Garf Hunter Dec 19 '16 at 3:48
• Can you give some test cases? Obviously don't post the answer to the PE one, but what about other examples? – Rɪᴋᴇʀ Dec 19 '16 at 3:55
• @WheatWizard: The word "or" implies that each number is counted only once, at most. I agree that the question needs to make it clear how many "numbers to check for multiples of" arguments must be supported, though. Exactly two? One or more? Zero or more? – smls Dec 19 '16 at 3:58
• Can we take "numbers equal to or below 10", or take 9 as input instead of 10? – Stewie Griffin Dec 19 '16 at 7:41
• "and a set of at least one positive integer A" how big can the set be? – betseg Dec 19 '16 at 7:41

# J, 18 bytes

[:+/@I.0=*/@(|/i.)


Try it online!

## Explanation

[:+/@I.0=*/@(|/i.)  Input: A (LHS), N (RHS)
i.   Make the range [0, 1, ..., n-1]
|/     Form the modulus table between each value in A and the range N
*/@(    )  Reduce columns by multiplication
0=           Test if each value if equal to 0, 1 if true else 0
[:   I.             Find the indicies of 1s
+/@               Reduce by addition and return


# PHP, 80 bytes

for($c=1;++$i<$argv;$c++?:$t+=$i)for($j=1;$v=$argv[++$j];$i%$v?:$c=0);echo$t;


Run from command line with -r. First argument is the limit N, any subsequent arguments are our positive integers A.

Our outer loop goes through the range 1..N, while the inner loop goes through each value of A and checks to see if $i is a multiple of it. If it is, we set a flag that tells our outer loop to add $i to our total ($t). We use a flag so that we don't add $i more than once. We then output our total at the end.

Clojure, 44 bytes

#(apply +(set(mapcat(partial range 0 %)%2)))


Instead of generating the whole range form 0 to N it is generating values on that range with step length of A_0, A_1, etc. and uses set to get unique values.

Calling convention:

(def f #(apply +(set(mapcat(partial range 0 %)%2))))
(f 50 [2 3 5])


Steps are more clear when ->> macro is used:

(defn f [N A] (->> A (mapcat #(range 0 N %)) set (apply +)))


Python 3 & 2.7, 55 bytes

Based on the same set idea as my Clojure version.

lambda N,A:sum(set(i for a in A for i in range(0,N,a)))


## Batch, 110 bytes

@set t=-%1
@for /l %%i in (1,1,%1)do @set p=1&(for %%e in (%*)do @set/ap*=%%e%%%%i)&set/at+=%%i*!p
@echo %t%


Takes input as command-line arguments; the first argument is n, the rest are the set of numbers. Works by computing the product of the remainders of all numbers up to and including n with each of the arguments including n itself and summing those numbers with a zero product; this causes n to always be included in the total so we offset that by starting the total at -n.

# C#, 92 bytes

n=>{int i=0,j,s=0;for(;i<n;i++)for(j=1;j<n.Length;)if(i%n[j++]<1){s+=i;break;}return s;};


Anonymous function which receives an array containing the upper limit and all required divisors.

This method does not use a set (actually HashSet in C#) since it was way too verbose. Not that this code is too succinct...

Full program and test cases:

using System;

public class Program
{
public static void Main()
{
Func<int[], int> f =
n =>
{
int i = 0, j, s = 0;
for ( ; i < n ; i++)
for (j = 1 ; j < n.Length ; )
if (i % n[j++] < 1)
{
s += i;
break;
}
return s;
};

// test cases:
Console.WriteLine(f(new int[]{50, 2})); // 600
Console.WriteLine(f(new int[]{10, 3, 5}));  // 23
Console.WriteLine(f(new int[]{28, 4, 2}));  // 182
Console.WriteLine(f(new int[]{19, 7, 5}));  // 51
Console.WriteLine(f(new int[]{50, 2, 3, 5}));   // 857
}
}


## awk, 64 bytes

{for(;i++<NF;j=0)while((c=$i*++j)&&c<$NF)a[c];for(i in a)$0+=i}1  Write the code to file program.awk and execute: $ echo run 2 3 5 50 | awk -f this.awk
857


run is a cludge to allow using record $0 for summing and shorter printing, emphasis on the latter. Try it online ## R, 39 bytes z=2:N-1;z%*%!!rowSums(!outer(z,X,"%%"))  N is the number, X is a vector of divisors ## Racket 94 bytes (apply +(remove-duplicates(for*/list((i(range 1(car l)))(j(cdr l))#:when(= 0(modulo i j)))i)))  Ungolfed: (define (f l) (apply + (remove-duplicates (for*/list ((i (range 1 (first l))) (j (rest l)) #:when (= 0 (modulo i j))) i))))  Testing: (f '(10 3 5)) (f '(50 2)) (f '(28 4 2)) (f '(19 7 5)) (f '(50 2 3 5))  Output: 23 600 182 51 857  # Perl 5-pa, 44 bytes map{$"="*$_%";$\+=!eval"$_%@F"&&$_}1..<>-1}{


Try it online!

Takes the list of A on the first line of input, space separated. Takes N on the second line.

# APL(NARS), 17 chars, 34 bytes

{+/∪⍺∼⍨∊⍵×⍳¨⌊⍺÷⍵}


test:

  f←{+/∪⍺∼⍨∊⍵×⍳¨⌊⍺÷⍵}
50 f 2
600
10 f 3 5
23
28 f 4 2
182
19 f 7 5
51
50 f 2 3 5
857