Hampir setara dengan pertanyaan pertama Project Euler:
Jika kita mendaftar semua bilangan alami di bawah 10 yang merupakan kelipatan 3 atau 5, kita mendapatkan 3, 5, 6 dan 9. Jumlah kelipatan ini adalah 23.
Temukan jumlah semua kelipatan 3 atau 5 di bawah 1000.
Tantangan:
Dengan bilangan bulat positif N
dan sekurang-kurangnya satu bilangan bulat positif A
, hasilkan jumlah dari semua bilangan bulat positif kurang dari N
itu adalah kelipatan dari setidaknya satu anggota A
.
Misalnya, untuk kasus Project Euler, inputnya adalah:
1000
3
5
Kasus uji:
Input : 50, [2]
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
Jawaban:
Jelly, 6 bytes
Try it online!
How it works
sumber
Python,
5955 bytesrepl.it
Unnamed function taking an integer,
n
and a list of integersl
. Traverses a range of the Natural numbers (plus zero) up to but not includingn
and sums (sum(...)
) those that have a remainder after division of zero (v%m<1
) forany
of the integersm
in the listl
. Uses multiplication rather than a conditional to save 3 bytes.sumber
Octave,
383633 bytesTake input as:
f(10, [3;5])
. This would be 2 bytes shorter if the input could bef(9,[3;5])
for the same test case.Verify all test cases here.
Explanation:
Octave can pre-decrement, so using
1:--x
instead of1:x-1
(two times) saves two bytes.mod(a,b)
gives1 2 0 1 2 0 1 2 0
formod(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 inputmod(1:9, [3;5])
this gives:Taking
~all(_,1)
on this givestrue
for the columns where at least one value is zero, andfalse
where all values are non-zero:The
,1
is needed in case there is only one number iny
. 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:
sumber
JavaScript (ES6),
403936 bytesInput: integer
n
and array of integer(s)a
with currying syntax(n)(a)
Test cases
Show code snippet
sumber
f=(n,a)=>n--&&a.some(v=>n%v<1)*n+f(n,a)
. Best I could do nonrecursively was 61 bytes.MATL, 9 bytes
Try it online!
sumber
1 2 ...
. You duplicate it and take modulus the other input. You negate it and multiply with the vector1 2 ..
, use unique to get rid of duplicates and finally summing it...Retina, 34 bytes
Byte count assumes ISO 8859-1 encoding.
Input format is
Try it online!
sumber
Python, 67 bytes
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.
sumber
x=y=0
on a separate line would save four bytes.Mathematica,
3727 bytesThanks to Martin Ender for a shrewd observation that led to big byte savings!
Unnamed function taking two arguments, a list
#
of integers (the desired divisorsA
) and an integer#2
(the upper boundN
) , and returning an integer.Range[#,#2-1,#]
gives, for each elementd
of the list#
, all the multiples ofd
less than or equal to#-1
(hence less than#
); the union of these lists is then computed and summed withTr
.Previous version:
sumber
Range
is listable:Tr[Union@@Range[#2,#-1,#2]]&
(and then save another byte by swapping the order of the inputs)Perl 6, 25 bytes
A lambda that takes the input numbers as arguments. (One argument for N, and an arbitrary number of arguments for A).
(Try it online.)
Explanation:
{ ... }
: A lambda.$^a
: First argument of the lambda.@_
: Remaining arguments of the lambda ("variadic parameter").^$^a
: Range from0
to$^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.sumber
^
to declare a placeholder parameter is fairly explicit. Especially since you could use it later in the block as just$a
. I think only$_
@_
%_
self
can ever be considered to be implicitly declared. I think I would have that line read "declare first parameter as a placeholder"@_
, and%_
in case of functions, are no different in that regard: They too only become part of the signature if they appear in the body. Only$_
(andself
and%_
in methods) can become part of a signature by default.R, 67 bytes
Takes a vector to STDIN in the following format:
[N, a_1, a_2, ...]
. Supports any number ofa
. For eacha
, creates the sequencea
toN-1
with stepsizea
. Then takes the sum of all the unique entries in that vector.sumber
Haskell,
4239 bytesUsage:
Thanks to @Zgarb for 3 bytes
sumber
(x`mod`)
is the same asmod x
.05AB1E, 9 bytes
Try it online!
sumber
à
(maximum) pops the list now, but didn't before.)Octave,
4937 bytesthe function will be called as
f([2 3 4],50)
Assume that
A=[2 3 4];
we require to have sum of numbers aswe can multiply
[2 3 4]
by1:50
to get matrix(1:N)'.*A
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)
function should be called as
f(unit64([2 3 4]),uint64(50))
sumber
Pyth, 10 bytes
Explanation
sumber
T-SQL, 87 bytes
This will work as long as
@i
has a value of 2048 or lowerTry it out
sumber
APL (Dyalog Unicode), 12 bytes
Try it online!
Anonymous tacit function. Thanks to @Adám for helping me shave 3 bytes off of this. Uses
⎕IO←0
.How:
sumber
Pip,
43 41 3935 bytesTry it online!
Explanation:
sumber
Python 2, 80 Bytes
This is very long. Can definitely be shortened. Taking the 3 numbers as separate inputs is definitely hurting the score.
sumber
x,y,z=input()
and give input in the form of(1000,3,5)
.Common Lisp, 77
Ungolfed
sumber
PowerShell, 57 bytes
Try it online!
Iterative solution. Takes input as a number
$a
and as a literal array$b
. Loops from1
up to one below$a
(via--$a
), using aWhere-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 outerWhere
is$true
and the current number is left on the pipeline. Otherwise, with no elements from$b
on the pipeline, the outerWhere
is$false
, so the current number is not placed on the pipeline.Those numbers are all gathered up in parens,
-join
ed together with+
signs, and piped to|iex
(short forInvoke-Expression
and similar toeval
). The summation result is left on the pipeline, and output is implicit.sumber
PHP,
787674 bytesThe 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
to0
if$i
is the multiple of any of them.Run with
-r
.sumber
Scala, 47 bytes
n is a List which contains of a first argument
N
, the rest are elements ofA
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.sumber
Java 8, 75 bytes
The method signature for this is
int f(int N, List<Integer> A)
sumber
Ruby,
52 4846 bytessumber
C11, 177 bytes
Requires this set of headers in the same folder, and the
fnv-hash
library found there as well. Compile likegcc 1.c ../fnv-hash/libfnv.a -o 1 -DNODEBUG
Test Program:
outputs
sumber
Japt
-x
,976 bytesTry it
sumber
Whispers v2, 178 bytes
Try it online!
Structure tree:
How it works
Very simply, we put each number (the lines withα denotes the set of numbers given as input:
Each
on them) through a series of functions (the lines withL
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, whereThis 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α to β according to the following rule:
whereαi denotes the i th element of α , and the same for β . Finally, we can simply take the sum of β , as the 0 elements do not affect the sum.
sumber
K (oK),
1514 bytesSolution:
Try it online!
Examples:
Explanation:
sumber
Actually, 13 bytes
Try it online!
Explanation:
sumber
Processing, 88 bytes
Uses the simple
for
-loop approach, sums all the multiples up and returns it. Input is the formatint
,int[]
array.sumber