Fast gleichbedeutend mit der ersten Frage von Project Euler:
Wenn wir alle natürlichen Zahlen unter 10 auflisten, die Vielfache von 3 oder 5 sind, erhalten wir 3, 5, 6 und 9. Die Summe dieser Vielfachen ist 23.
Ermitteln Sie die Summe aller Vielfachen von 3 oder 5 unter 1000.
Herausforderung:
Bei einer positiven Ganzzahl N
und einer Menge von mindestens einer positiven Ganzzahl A
wird die Summe aller positiven Ganzzahlen ausgegeben, die kleiner als N
das Vielfache von mindestens einem Element von sind A
.
Für den Projekt-Euler-Fall wäre die Eingabe beispielsweise:
1000
3
5
Testfälle:
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
Antworten:
Gelee , 6 Bytes
Probieren Sie es online!
Wie es funktioniert
quelle
Python,
59-55Bytesrepl.it
Unbenannte Funktion mit einer Ganzzahl
n
und einer Liste von Ganzzahlenl
. Durchläuft einen Bereich der Natural-Zahlen (plus Null) bis einschließlichn
und summiert (sum(...)
) diejenigen, die nach Division von Null (v%m<1
) fürany
die Ganzzahlenm
in der Liste einen Rest habenl
. Verwendet Multiplikation anstelle einer Bedingung, um 3 Bytes zu sparen.quelle
Oktave,
383633 BytesNehmen Eingang als:
f(10, [3;5])
. Dies wäre 2 Byte kürzer, wenn die Eingabef(9,[3;5])
für denselben Testfall erfolgen könnte.Überprüfen Sie hier alle Testfälle.
Erläuterung:
Octave kann vorab dekrementiert werden, also mit
1:--x
statt1:x-1
(zweimal) verwenden, werden zwei Bytes gespeichert.mod(a,b)
gibt1 2 0 1 2 0 1 2 0
fürmod(1:9,3)
. Wenn das zweite Argument ein vertikaler Vektor ist, wird die erste Eingabe vertikal repliziert und der Modul für jeden der Werte im zweiten Eingabeargument verwendet. Also zur Eingabemod(1:9, [3;5])
ergibt sich also:Wenn Sie
~all(_,1)
dies annehmen, erhalten Sietrue
für die Spalten, in denen mindestens ein Wert Null ist, undfalse
denen alle Werte ungleich Null sind:Das
,1
wird benötigt, falls nur eine Nummer eingegeben wurdey
. Andernfalls würde es auf den gesamten Vektor anstatt auf Nummer für Nummer wirken.Wenn Sie dies in eine vertikale Matrix umwandeln und die Matrixmultiplikation verwenden, erhalten Sie die richtige Antwort, ohne dass eine explizite Summierung erforderlich ist:
quelle
JavaScript (ES6),
403936 ByteEingabe: Integer
n
und Array von Integer (s)a
mit aktueller Syntax(n)(a)
Testfälle
Code-Snippet anzeigen
quelle
f=(n,a)=>n--&&a.some(v=>n%v<1)*n+f(n,a)
. Das Beste, was ich nicht rekursiv machen konnte, waren 61 Bytes.MATL , 9 Bytes
Probieren Sie es online!
quelle
1 2 ...
. Du duplizierst es und nimmst Modul für den anderen Eingang. Sie negieren es und multiplizieren mit dem Vektor1 2 ..
, verwenden Unique, um Duplikate loszuwerden und es schließlich zu summieren ...Retina , 34 Bytes
Die Anzahl der Bytes setzt die Kodierung nach ISO 8859-1 voraus.
Eingabeformat ist
Probieren Sie es online!
quelle
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.
quelle
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:
quelle
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.quelle
^
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.quelle
Haskell,
4239 bytesUsage:
Thanks to @Zgarb for 3 bytes
quelle
(x`mod`)
is the same asmod x
.05AB1E, 9 bytes
Try it online!
quelle
à
(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))
quelle
Pyth, 10 bytes
Explanation
quelle
T-SQL, 87 bytes
This will work as long as
@i
has a value of 2048 or lowerTry it out
quelle
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:
quelle
Pip,
43 41 3935 bytesTry it online!
Explanation:
quelle
Python 2, 80 Bytes
This is very long. Can definitely be shortened. Taking the 3 numbers as separate inputs is definitely hurting the score.
quelle
x,y,z=input()
and give input in the form of(1000,3,5)
.Common Lisp, 77
Ungolfed
quelle
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.quelle
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
.quelle
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.quelle
Java 8, 75 bytes
The method signature for this is
int f(int N, List<Integer> A)
quelle
Ruby,
52 4846 bytesquelle
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
quelle
Japt
-x
,976 bytesTry it
quelle
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.
quelle
K (oK),
1514 bytesSolution:
Try it online!
Examples:
Explanation:
quelle
Actually, 13 bytes
Try it online!
Explanation:
quelle
Processing, 88 bytes
Uses the simple
for
-loop approach, sums all the multiples up and returns it. Input is the formatint
,int[]
array.quelle