| Bytes | Lang | Time | Link |
|---|---|---|---|
| 014 | Husk | 240913T140351Z | int 21h |
| 031 | Julia 1.0 | 230824T155323Z | Ashlin H |
| 074 | Headass | 230825T032046Z | thejonym |
| 013 | Itr | 230803T185534Z | bsoelch |
| 052 | Rust | 230804T092312Z | Jeremy M |
| 004 | Japt fx | 181104T200602Z | Shaggy |
| 007 | Nekomata | 230711T014758Z | alephalp |
| 005 | Thunno 2 S | 230708T183037Z | The Thon |
| 009 | Pyt | 230109T010426Z | Kip the |
| nan | Fig | 221013T044154Z | tybocopp |
| 005 | Vyxal s | 221010T020524Z | tybocopp |
| 041 | Scala | 200803T212054Z | user |
| 2725 | x8616 machine code | 200812T171146Z | 640KB |
| 015 | Arn | 200812T143622Z | ZippyMag |
| 090 | Google Sheets | 200730T193433Z | General |
| 063 | Io | 200730T063027Z | user9649 |
| 012 | Pip | 200726T055243Z | DLosc |
| 035 | Pip | 190627T152225Z | Kenzie |
| 034 | APLNARS | 181105T200428Z | user5898 |
| 012 | APL Dyalog Unicode | 190627T221603Z | J. Sall& |
| 044 | Perl 5 pa | 190627T172237Z | Xcali |
| 014 | K oK | 181111T220641Z | mkst |
| 178 | Whispers v2 | 181111T213858Z | caird co |
| 177 | C11 | 161224T023540Z | cat |
| 094 | Racket | 161221T155515Z | rnso |
| 046 | Ruby | 161221T154004Z | G B |
| 039 | R | 161219T202154Z | skan |
| 064 | awk | 161221T145319Z | James Br |
| 075 | Java 8 | 161220T155258Z | Nonlinea |
| 025 | Perl 6 | 161219T035008Z | smls |
| 092 | C# | 161220T150319Z | adrianmp |
| 047 | Scala | 161220T113118Z | sprague4 |
| 037 | Octave | 161219T104444Z | rahnema1 |
| 110 | Batch | 161220T013916Z | Neil |
| 055 | Python 3 & 2.7 | 161219T232416Z | NikoNyrh |
| 044 | Clojure | 161219T231349Z | NikoNyrh |
| 027 | Mathematica | 161219T090108Z | Greg Mar |
| 009 | 05AB1E | 161219T193534Z | Osable |
| 074 | PHP | 161219T170014Z | Titus |
| 067 | Python | 161219T041718Z | Riker |
| 057 | PowerShell | 161219T161554Z | AdmBorkB |
| 080 | PHP | 161219T140109Z | Xanderha |
| 077 | Common Lisp | 161219T130545Z | coredump |
| 087 | TSQL | 161219T120538Z | t-clause |
| 039 | Haskell | 161219T064333Z | Angs |
| 018 | J | 161219T103842Z | miles |
| 034 | Retina | 161219T103333Z | Martin E |
| 009 | MATL | 161219T102530Z | Luis Men |
| 036 | JavaScript ES6 | 161219T074832Z | Arnauld |
| 033 | Octave | 161219T074918Z | Stewie G |
| 067 | R | 161219T091536Z | JAD |
| 088 | Processing | 161219T085202Z | user4180 |
| 055 | Python | 161219T080417Z | Jonathan |
| 013 | Actually | 161219T062617Z | user4594 |
| 006 | Jelly | 161219T061719Z | Dennis |
| 010 | Pyth | 161219T034748Z | user4854 |
| 080 | Python 2 | 161219T034225Z | Theo |
Husk, 14 bytes
ΣuΣzom*⁰Ṫ¦⁰tŀ²
I must admit that the handling of two lists (a number range and the list of divisors) was quite difficult and, subsequently, this code might look quite long.
This program takes a number and a list of divisors as an input and outputs the sum of all multiples the input list.
Explanation:
ŀ² # Make a range 0 to input number minus 1
t # Drop 0
Ṫ¦⁰ # Make a table of divisors:
# if the value is divisible, put quotient, otherwise put 0
z # Use zip to get the dividends back
om*⁰ # By multiplying with the list of divisors (zeroes stay zeroes)
Σ # Simplify the list
u # Deduplicate
Σ # Calculate the total sum
Julia 1.0, 31 bytes
N^A=sum(x->any(@.x%A<1)x,1:N-1)
The function sum can accept a subfunction to be called on each element before summation. I use this to multiply each element x in the range \$[1, N-1]\$ by the boolean value any(@.x%A<1), essentially filtering the range. Julia supports logical indexing for arrays, but this generally requires defining the array first, which often isn't ideal for code golf.
The expression @. vectorizes the functions that follow, but in this case, it doesn't save any bytes over the alternative, x.%A.<1.
-1 byte thanks to MarcMush: use the result of any as a literal coefficient
Headass, 74 bytes
{N)OUO}:+E.UO^UO[{UON)}:](D)RP:N+E.U+O-[UOU{R)U-ORO^:R-OUO;UN)}:D)R:R];ONE
Try It Online! - Input is taken as a newline delimited list of numbers, with the first one being N, and the rest being the values of A
tattoo on my forehead that says I LOVE EASY CHALLENGES
Explanation
PREAMBLE: this program counts up from 0 to N, with this increasing value being
referred to as the "counter". along the way it reads members of A, which
will be given "countdown timers", which start at 0 and are reset to the
associated A member value minus one, so that every A[i]th cycle, they reach
0 and trigger the adding of the counter to the accumulator.
The accumulator, of course, accumulatizes.
{N)OUO}:+E. code block 0 (initialization)
{N) }: for each input
OUO save 0, save input
this inits the counter, stores N,
creates a list of countdown timers starting at 0
and implicitly, an accumulator starting at 0
all on the queue. the queue now looks like this:
0 N 0 A1 0 A2 0 A3... 0
+E go to code block 1
. end code block
UO^UO[{UON)}:](D)RP:N+E. code block 1 (checking if we're done)
UO^ set r1 to the counter value
UO[ set r2 to N
{UON)}: put the rest of the values back on the queue,
noting that the accumulator is the last value
](D) if r1 == r2 (the counter has reached N)
RP print the last value added to the queue
(which is the accumulator)
(and halt)
: else
N+E go to code block 2
. end code block
U+O-[UOU{R)U-ORO^:R-OUO;UN)}:D)R:R];ONE code block 2
U+O put the counter + 1 onto the queue
U+ -[ set r2 to the counter value
UO put N back on the queue
{ N)}: for each member of A
U R) U if the countdown timer has reached 0
U-O reset the countdown timer
RO put the A member back on the queue
^ raise a flag in r1
: else
R-O decrement the countdown timer
UO put the A member back on the queue
; endif
D) if the flag in r1 is down
R: ;O put the accumulator back on the queue
: else
R];O increase the accumulator by the
value of the counter
NE return to code block 1
There's a bunch of implicit re-adding of values to the queue that I don't mention, basically any time I say that I've modified some value, that means I'm saving the modified version to the queue.
Also, registers r0-r3 are reset between code blocks, so you can basically think about them as local variables in each code block. Register r0 is constantly in use but not mentioned due to the language structure, and register r3 is in use whenever ( or ) show up, but it's much easier to abstract these to simple comparisons, since that's all they're used for in this program.
I'm still always learning the best way to explain this language, trying to balance between overexplaining and overabstracting, if anyone has any questions of course feel free to ask, you may have a golf on your hands :P
Itr, 13 bytes
#º#Xáà%¬*«ÍÌS
Explanation
# ; read the number
º ; convert it to a zero-based range
# ; read the list of numbers
× « ; execute for each pair of numbers in the input
áà ; duplicate the lower number in the pair
%¬ ; check for divisibility
* ; keep the number if it is divisible
ÍÌ ; uniquify the list
S ; sum up the elements
Itr, 11 bytes
using implicit IO
ºXâà%¬*«ÍÌS
Nekomata, 7 bytes
ᵒ%ᵐ∏¬x∙
ᵒ%ᵐ∏¬x∙
ᵒ% Generate a modulo table
ᵐ∏ Take product of each row
¬ Logical not
x∙ Dot product with range from 0 to length - 1
Thunno 2 S, 5 bytes
Læ¹ḊS
Thunno 2 S, 6 bytes
LsȷḊʂV
Explanations
Læ¹ḊS # Implicit input -> [2,3,5], 50
L # Lowered range -> [2,3,5], [0..49]
æ # Filtered by: -> 0 1 ... 48 49
¹Ḋ # Divisible by 2nd input -> [1,1,1] [0,0,0] [1,1,0] [0,0,0]
S # Sum (any are true) -> 3 0 ... 2 0
# (End filter) -> [0,2,3,4,5,...,42,44,45,46,48]
# (S flag) Sum the list -> 857
# Implicit output
LsȷḊʂV # Implicit input
# [2,3,5], 50
L # Take the lowered range of the first input
# [2,3,5], [0..49]
s # Swap the two values on the stack
# [0..49], [2,3,5]
ȷḊ # Outer product over divisibility check
# [[0%2==0, 0%3==0, 0%5==0], ..., [49%2==0, 49%3==0, 49%5==0]]
# [[1,1,1], [0,0,0], [1,0,0], ..., [0,0,0], [1,1,0], [0,0,0]]
ʂ # Sum each inner list (check if any are true)
# [3,0,1,1,1,...,2,1,0,2,0]
V # Get the indices of truthy (non-zero) items
# [0,2,3,4,5,...,42,44,45,46,48]
# (S flag) Sum the resulting list
# 857
# Implicit output
Pyt, 9 bytes
⁻řĐ←ΠǤ1>·
⁻ř implicit input (N); decrement and push [1,2,...,N-1]
Đ duplicate top of stack
←Π get the product of the members of A
Ǥ GCD (elementwise)
1> is each GCD greater than 1?
· dot product; implicit print
Fig, \$11\log_{256}(96)\approx\$ 9.054 bytes
VxSFrx'!A%v
-8.232 bytes thanks to @Sʨɠɠan
Explanation:
Vx # Set the register to the first input (the list)
rx # Range [0, n)
F ' # Filter:
% # Modulo
v # The register
A # All truthy
! # Logical not
S # Sum
Vyxal s, 5 bytes
ɽ'⁰Ḋa
Explanation:
ɽ # Range 1 to n-1
' # Keep those that are
Ḋ # Divisible by
a # Any of
⁰ # The second input
# Sum with the s flag
Scala, 45 41 bytes
s=>0 to _-1 filter(x=>s.exists(x%_<1))sum
Takes as its input a List[Int] s and an Int, creates a range from 0 to that number, filters every number in that range that's divisible by an element in s, and sums it.
Requires language:postfixOps.
x86-16 machine code, 27 25 bytes
Binary:
00000000: 33db 4a51 56ac a20c 018a c2d4 0ae0 f675 3.JQV..........u
00000010: 0203 da5e 594a 75eb c3 ...^YJu..
Listing:
33 DB XOR BX, BX ; clear BX running sum
4A DEC DX ; N = N - 1 (only multiples below N)
N_LOOP:
51 PUSH CX ; save array length
56 PUSH SI ; save array start pointer
F_LOOP:
AC LODSB ; AL = next factor
A2 0C01 MOV BYTE PTR[AAM1+1], AL ; AAM divisor = AL
8A C2 MOV AL, DL ; AL = dividend
AAM1:
D4 0A AAM ; ZF = ( current N % factor == 0 )
E0 F6 LOOPNZ F_LOOP ; if not a multiple, continue to next factor
75 02 JNZ F_BREAK ; if last is not a multiple, don't add
03 DA ADD BX, DX ; otherwise add factor to sum
F_BREAK:
5E POP SI ; restore array pointer
59 POP CX ; restore array length
4A DEC DX ; decrement N / loop counter
75 EB JNZ N_LOOP ; loop until N = 0
C3 RET ; return to caller
As a function: input N in DL, SI array of factors, CX array size.
Once again, using DOS DEBUG to test. Note: literal values are in hex in DEBUG.
N = 10 (0xA), [ 3, 5 ] = 23 (0x17)
N = 50 (0x32), [ 2, 3, 5 ] = 857 (0x359)
Arn, 15 bytes
pIÔX╗▀‹·¬○¯vËa?
Explained
Unpacked: +\$v{$:{!v%}?1}1->
\ Fold with...
+ ...addition
$ Filter with...
v{ ...block with key of v
$: Any operation
{ Block with key of _
! Boolean not
v% v modulo _ (implied)
}
?1 Each element on second line of _ (implied)
}
1-> Range, [1, _)
When passing an array in string or integer context, Arn will automatically take the 0th element. When passing a string/integer in array context, Arn will split on spaces if there are any or every character otherwise.
STDIN is initialized to the _ variable, split on newlines if there are any or just a string if one line.
Originally I had +\$v{$:{0=v%}?1}1->, which was 1 byte longer.
Google Sheets, 141 140 134 132 90
Closing parens already discounted.
Phew! That was more work than I bargained for. The hard part was getting something that worked for all ranges.
Inputs:
A1- N- Row
2- A1, A2, ...
Formulae:
A3-=A1-1(N - 1)A4-=SUM(ArrayFormula(SEQUENCE(A3)*LT(,COUNTIF(MMULT(SEQUENCE(A1),FILTER(2:2,2:2)),SEQUENCE(A3)))))
Where A4 is the final output.
How It Works
Hint: Start from the middle.
# Sum everything together
=SUM(
ArrayFormula(
# The "number under N"
SEQUENCE(A3)*
# Use the LT Operator function to pile parens at the end
LT(
# Implicit 0
,
COUNTIF(
# Generate every multiple of each element in A via matrix multiplication up to the Nth multiple.
# This is overkill, but saves characters.
MMULT(
# Column Matrix `(1, ... , N)` and Row Matrix `(A1, A2, ...)`.
SEQUENCE(A1),
# Have to remove blanks from the end
FILTER(2:2,2:2)
),
# If element appears in the resulting matrix, multiply by 1, or else 0
SEQUENCE(A3)
)
)
)
)
Io, 63 bytes
Port of NikoNyrh's answer.
method(N,A,A map(i,Range 0toBy(N-1,i)asList)flatten unique sum)
Explanation
method(N,A, ) // Take two inputs, the number and the array
A map(i, ) // For each item in the array A:
Range 0toBy(N-1,i) // Generate a range from 0 to N-1, with the step of i
asList // Convert it to a list
flatten // Flatten the lists
unique // Uniquify them
sum // Sum the resulting list
Pip, 12 bytes
$+Y0N_%gFI,q
Explanation
Uses a somewhat unusual input method: takes N from stdin and the set of divisors as command-line args.
$+Y0N_%gFI,q
g is list of command-line args (implicit)
q Read a line of stdin
, Range from 0 up to one less than that value
FI Filter on this function:
_ Take each element
%g mod the list of divisors g
0N Check if zero is in the result list: some number in g divides the element exactly
Result: a list of the numbers that are a multiple of one of the set of divisors
Y Yank (no-op to manipulate operator precedence)
$+ Fold on addition
Pip, 43 41 39 35 bytes
b^:sFc,a{f:0Fdb{f?0c%d?0(f:i+:c)}}i
Explanation:
Takes inputs like so:
arg1 1000
arg2 3 5
b^:s ;read rest of inputs as array
;(s is " " and ^ is split into array on char)
F c ,a{ ;for(c in range(0,a))
f:0 ;flag to prevent double counting 15,30,etc.
F d b { ;forEach(d in b)
f? 0 c%d? 0 (f:i+:c) ;if flag {continue}elif c%d {f=i+=c}
; (i will always be truthy so why not)
}
}
i ;print sum
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
APL (Dyalog Unicode), 12 bytes
+/⊢∘⍳∩∘∊×∘⍳¨
Anonymous tacit function. Thanks to @Adám for helping me shave 3 bytes off of this. Uses ⎕IO←0.
How:
+/⊢∘⍳∩∘∊×∘⍳¨ ⍝ Tacit function. Left and right arguments will be called ⍺ and ⍵ respectively.
×∘⍳¨ ⍝ Multiply ⍺ with each element of [0..⍵-1]
∊ ⍝ Enlist (flattens the vector)
∩∘ ⍝ Then, get the intersection of that vector with
⊢∘⍳ ⍝ The vector [0..⍵-1].
+/ ⍝ Then sum
Perl 5 -pa, 44 bytes
map{$"="*$_%";$\+=!eval"$_%@F"&&$_}1..<>-1}{
Takes the list of A on the first line of input, space separated. Takes N on the second line.
K (oK), 15 14 bytes
Solution:
{+/&|/~y!\:!x}
Examples:
{+/&|/~y!\:!x}[50;,2]
600
{+/&|/~y!\:!x}[10;3 5]
23
Explanation:
{+/&|/~y!\:!x} / the solution
{ } / lambda taking implicit x and y
!x / range 0..x-1
y!\: / modulo (!) x with each-left (\:) item in y
~ / not
|/ / min-over to flatten into single list
& / indices where true
+/ / sum up
Whispers v2, 178 bytes
> Input
> Input
> ℕ
>> (1)
>> ∤L
>> {L}
>> L∩2
>> #L
>> L∈3
>> L⋅R
>> Each 5 4
>> Each 6 11
>> Each 7 12
>> Each 8 13
>> Each 9 14
>> Each 10 15 4
>> ∑16
>> Output 17
Structure tree:
How it works
Very simply, we put each number (the lines with Each on them) through a series of functions (the lines with L on them), then, based off the results of those functions, we discard some numbers and keep the rest, before finally summing them. In fact, we can define those functions, where \$\alpha\$ denotes the set of numbers given as input:
\begin{align} f(x) & = \{i \: | \: (i|x), i \in \mathbb{N}\} & \text{i.e. the set of divisors of} \: x \\ g(x) & = f(x) \cup \alpha & \text{i.e. the union of the divisors of} \: x \: \text{with} \: \alpha \\ h(x) & = |g(x)| > 0 & \text{i.e.} \: g(x) \: \text{is not empty} \end{align}
This is what lines 5 through to 10 represent. Lines 11 through 16 are simply the application of those three functions. Once we've defined all the functions, we then mutate \$\alpha\$ to \$\beta\$ according to the following rule:
$$\beta_i = \begin{cases} \alpha_i & h(\alpha_i) \: \top \\ 0 & h(\alpha_i) \: \bot \end{cases}$$
where \$\alpha_i\$ denotes the \$i\$th element of \$\alpha\$, and the same for \$\beta\$. Finally, we can simply take the sum of \$\beta\$, as the \$0\$ elements do not affect the sum.
C11, 177 bytes
#include"object.h"
#define S size_t
S g(S m,array_t*d){S s,i,l,j;for(s=i=0;i<m;i++){for(l=1,j=0;j<d->idx+1;l*=i%(S)(*array_get_ref(d,j++,NULL))->fwi->value);s+=l?0:i;}return s;}
Requires this set of headers in the same folder, and the fnv-hash library found there as well. Compile like gcc 1.c ../fnv-hash/libfnv.a -o 1 -DNODEBUG
Test Program:
#include "../calc/object/object.h"
#include <stdio.h>
size_t f (const size_t max, const size_t a, const size_t b);
size_t f2 (const size_t max, const array_t* const divs);
size_t g (size_t max, array_t* divs);
define_array_new_fromctype(size_t);
int main(void) {
printf("%zu\n", f(10, 3, 5));
static const size_t a[] = {
3, 5
};
array_t* b = array_new_from_size_t_lit(a, 2, t_realuint);
printf("%zu\n", f2(10, b));
printf("%zu\n", g(10, b));
array_destruct(b);
return 0;
}
size_t f (const size_t max, const size_t a, const size_t b) {
size_t sum = 0;
for (size_t i = 0; i < max; i++) {
sum += (i % a * i % b) ? 0 : i;
}
return sum;
}
size_t f2 (const size_t max, const array_t* const divs) {
size_t sum = 0;
const size_t len = array_length(divs);
for (size_t i = 0; i < max; i++) {
size_t mul = 1;
for (size_t j = 0; j < len; j++) {
object_t** this = array_get_ref(divs, j, NULL);
fixwid_t* num = (*this)->fwi;
mul *= i % (size_t) num->value;
}
sum += mul ? 0 : i;
}
return sum;
}
#define S size_t
S g(S m,array_t*d){S s,i,l,j;for(s=i=0;i<m;i++){for(l=1,j=0;j<d->idx+1;l*=i%(S)(*array_get_ref(d,j++,NULL))->fwi->value);s+=l?0:i;}return s;}
outputs
23
23
23
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
Ruby, 52 48 46 bytes
->b{b[s=0].times{|x|b.find{|y|x%y<1&&s+=x}};s}
R, 39 bytes
z=2:N-1;z%*%!!rowSums(!outer(z,X,"%%"))
N is the number, X is a vector of divisors
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.
Java 8, 75 bytes
(N,A)->IntStream.range(1,N).filter(x->A.stream().anyMatch(y->x%y==0)).sum()
The method signature for this is int f(int N, List<Integer> A)
Perl 6, 25 bytes
{sum grep *%%@_.any,^$^a}
A lambda that takes the input numbers as arguments. (One argument for N, and an arbitrary number of arguments for A).
Explanation:
{ ... }: A lambda.$^a: First argument of the lambda.@_: Remaining arguments of the lambda ("variadic parameter").^$^a: Range from0to$^a - 1.* %% @_.any: Another lambda, which tests its argument*using the divisible-by operator%%against anany-Junction of the list@_.grep PREDICATE, RANGE: iterates the range of numbers and returns the ones for which the predicate is true.
C#, 92 bytes
n=>{int i=0,j,s=0;for(;i<n[0];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[0] ; 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
}
}
Scala, 47 bytes
n=>1.to(n(0)-1).filter(i=>n.exists(i%_==0)).sum
n is a List which contains of a first argument N, the rest are elements of A
Works by filtering out numbers where there doesn't exist at least one A of which i is a multiple, then summing. Strictly speaking we should use n.tail.exists inside the closure, but as i is always less than N and therefore never a multiple of N the solution is still complete without this.
Octave, 49 37 bytes
@(A,N)sum(unique((z=(1:N)'.*A)(z<N)))
the function will be called as f([2 3 4],50)
Assume that A=[2 3 4]; we require to have sum of numbers as
sum(
2,4,6...,50-1 ,
3,6,9...,50-1,
4,8,12,...50-1)
we can multiply [2 3 4] by 1:50 to get matrix (1:N)'.*A
[2 4 6 ... 2*50
3 6 9 ... 3*50
4 8 12 ...4*50]
then extract from the matrix those that are smaller than 50 : z(z<N)
Since there are repeated elements in the matrix we extract unique values and sum them.
previous answer: (this solution will fail if N==1)
@(A,N)sum((k=uint64(1:N-1))(any(k==(k./A').*A')))
function should be called as f(unit64([2 3 4]),uint64(50))
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.
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)))
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 +)))
Mathematica, 37 27 bytes
Thanks to Martin Ender for a shrewd observation that led to big byte savings!
Tr[Union@@Range[#,#2-1,#]]&
Unnamed function taking two arguments, a list # of integers (the desired divisors A) and an integer #2 (the upper bound N) , and returning an integer. Range[#,#2-1,#] gives, for each element d of the list #, all the multiples of d less than or equal to #-1 (hence less than #); the union of these lists is then computed and summed with Tr.
Previous version:
Tr[x=#;Union@@(Range[#,x-1,#]&/@#2)]&
05AB1E, 9 bytes
FND²%P_*O
F For N in [0, ..., input[0]-1]
ND²% Evaluate N%input[1]; yields an array of results
P Take the total product of the array. Yields 0 only if at least one of the value is 0, in other words if N is multiple of at least one of the specified values
_ Boolean negation, yields 1 if the last value is 0 and yields 0 otherwise
* Multiply by N: yields N if the last value is 0 and yields 0 otherwise
O Display the total sum
PHP, 78 76 74 bytes
for(;++$i<$argv[$f=$k=1];$s+=$i*!$f)for(;$v=$argv[++$k];)$f*=$i%$v;echo$s;
The outer loop runs $i from 1 to below first argument and adds $i to $s if $f is not set.
The inner loop multiplies $f with ($i modulo argument) for all subsequent arguments, setting $f to 0 if $i is the multiple of any of them.
Run with -r.
Python, 67 bytes
a,b,c=input()
x=y=0
exec("if x%c<1or 1>x%b:y+=x\nx+=1\n"*a)
print y
After writing this I noticed my code was similar to the existing python answer, however I came up with it independently and am posting it anyway.
PowerShell, 57 bytes
param($a,$b)(1..--$a|?{$i=$_;$b|?{!($i%$_)}})-join'+'|iex
Iterative solution. Takes input as a number $a and as a literal array $b. Loops from 1 up to one below $a (via --$a), using a Where-Object operator |?{...} with a clause to select certain numbers.
The clause sets $i to be the current number before sending input array $b into another |?{...}, here picking out those items where the current number is evenly divided by at least one of the numbers in $b. Those elements of $b that do divide evenly are left on the pipeline.
Thus, if there is at least one element from $b, the pipeline contains an element, so the outer Where is $true and the current number is left on the pipeline. Otherwise, with no elements from $b on the pipeline, the outer Where is $false, so the current number is not placed on the pipeline.
Those numbers are all gathered up in parens, -joined together with + signs, and piped to |iex (short for Invoke-Expression and similar to eval). The summation result is left on the pipeline, and output is implicit.
PHP, 80 bytes
for($c=1;++$i<$argv[1];$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.
Common Lisp, 77
(lambda(n x)(loop for i below n when(some(lambda(u)(zerop(mod i u)))x)sum i))
Ungolfed
(lambda (limit seeds)
(loop for i below limit
when (some (lambda (u) (zerop (mod i u))) seeds)
sum i))
T-SQL, 87 bytes
This will work as long as @i has a value of 2048 or lower
USE master--needed for databases not using master as default
DECLARE @i INT=50
DECLARE @ table(a int)
INSERT @ values(2),(3),(5)
SELECT sum(distinct number)FROM spt_values,@ WHERE number%a=0and abs(number)<@i
Haskell, 42 39 bytes
a!b=sum[x|x<-[1..a-1],any((<1).mod x)b]
Usage:
Main> 50![2,3,5]
857
Thanks to @Zgarb for 3 bytes
J, 18 bytes
[:+/@I.0=*/@(|/i.)
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
Retina, 34 bytes
Byte count assumes ISO 8859-1 encoding.
\d+
$*
M&!`(.+)\1*(?=1¶.*\b\1\b)
1
Input format is
50
2,3,5
JavaScript (ES6), 40 39 36 bytes
Input: integer n and array of integer(s) a with currying syntax (n)(a)
n=>F=a=>n--&&!a.every(v=>n%v)*n+F(a)
Test cases
let f =
n=>F=a=>n--&&!a.every(v=>n%v)*n+F(a)
console.log(f(50)([2])); // 600
console.log(f(10)([3, 5])); // 23
console.log(f(28)([4, 2])); // 182
console.log(f(19)([7, 5])); // 51
console.log(f(50)([2, 3, 5])); // 857
Octave, 38 36 33 bytes
@(x,y)(1:--x)*~all(mod(1:x,y),1)'
Take input as: f(10, [3;5]). This would be 2 bytes shorter if the input could be f(9,[3;5]) for the same test case.
Explanation:
@(x,y) % Anonymous function that takes two inputs, x and y
% x is a scalar and y is a vertical vector with the set of numbers
(1:--x)* % Pre-decrement x and create a vector 1 2 ... x-1
Octave can pre-decrement, so using 1:--x instead of 1:x-1 (two times) saves two bytes.
mod(a,b) gives 1 2 0 1 2 0 1 2 0 for mod(1:9,3). If the second argument is a vertical vector, it will replicate the first input vertically and take the modulus for each of the values in the second input argument. So, for input mod(1:9, [3;5]) this gives:
1 2 0 1 2 0 1 2 0
1 2 3 4 0 1 2 3 4
Taking ~all(_,1) on this gives true for the columns where at least one value is zero, and false where all values are non-zero:
~all(mod(1:x,y),1)
0 0 1 0 1 1 0 0 1
The ,1 is needed in case there is only one number in y. Otherwise it would act on the entire vector instead of number-by-number.
Transposing this to a vertical matrix and use matrix multiplication, will give us the correct answer, without the need for explicit summing:
R, 67 bytes
a=scan();x=c();for(i in a[-1])x=c(x,seq(i,a[1]-1,i));sum(unique(x))
Takes a vector to STDIN in the following format: [N, a_1, a_2, ...]. Supports any number of a. For each a, creates the sequence a to N-1 with stepsize a. Then takes the sum of all the unique entries in that vector.
Processing, 88 bytes
int q(int a,int[]b){int s=0,i=0;for(;++i<a;)for(int j:b)if(i%j<1){s+=i;break;}return s;}
Uses the simple for-loop approach, sums all the multiples up and returns it. Input is the format int, int[]array.
Python, 59 55 bytes
lambda n,l:sum(v*any(v%m<1for m in l)for v in range(n))
Unnamed function taking an integer, n and a list of integers l. Traverses a range of the Natural numbers (plus zero) up to but not including n and sums (sum(...)) those that have a remainder after division of zero (v%m<1) for any of the integers m in the list l. Uses multiplication rather than a conditional to save 3 bytes.
Actually, 13 bytes
DR∙`i;)%Y*`MΣ
Explanation:
DR∙`i;)%Y*`MΣ
DR range(1, N)
∙ Cartesian product with A
`i;)%Y*`M for each pair:
i;) flatten, make a copy of the value from the range
%Y test if value from range divides value from A
* value from range if above is true else 0
Σ sum
Jelly, 6 bytes
ḍþṖḅTS
How it works
ḍþṖḅTS Main link. Left argument: D (array). Right argument: n (integer)
ḍþ Divisible table; test each k in [1, ..., n] for divisibility by all
integers d in D.
Ṗ Pop; discard the last Boolean array, which corresponds to n.
ḅ Unbase; convert the Boolean arrays of base n to integer. This yields a
non-zero value (truthy) and and only if the corresponding integer k is
divisible by at least one d in D.
T Truth; yield the array of all indices of truthy elements.
S Compute their sum.
Pyth, 10 bytes
s{sm:0hQdt
Explanation
s{sm:0hQdtQ Implicit input
:0hQd Get multiples of d below the bound
m tQ ... for each d given
s Concatenate results
{ Remove repeats
s Take the sum
Python 2, 80 Bytes
This is very long. Can definitely be shortened. Taking the 3 numbers as separate inputs is definitely hurting the score.
i=input
x=i();y=i();z=i();s=c=0
exec("if c%z<1 or c%y<1:s+=c\nc+=1\n"*x)
print s


