#K38392. Sum of Multiples
Sum of Multiples
Sum of Multiples
Given a list of integers and a number n (threshold), calculate the sum of all numbers in the list that are strictly less than n and are multiples of 3 or 5. In other words, for each integer \(x\) in the list, if \(x < n\) and either \(3\) divides \(x\) or \(5\) divides \(x\), then \(x\) is added to the sum.
Input and Output are provided via standard input and output respectively. Read the input values according to the format described below and output the computed sum.
The problem can be formally represented as:
[ \text{result} = \sum_{\substack{x \in \text{list} \ x < n \text{ and } (3\mid x \text{ or } 5\mid x)}} x ]
inputFormat
The input is read from standard input. The first line contains an integer representing the number of elements in the list, say m. The second line contains m space-separated integers. The third line contains the threshold integer n.
outputFormat
Output a single integer, which is the sum of all elements in the list that are less than n and are divisible by either 3 or 5. The output should be printed to standard output.## sample
8
2 4 6 8 10 12 15 18
12
16