We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #1: Multiples of 3 and 5
Project Euler #1: Multiples of 3 and 5
Sort by
recency
|
1469 Discussions
|
Please Login in order to post a comment
t = int(input().strip()) s=[] for a0 in range(t): n = int(input().strip()) s.append(n) u=s[0] v=s[1] s1=[] s2=[] for j in range(1,u): if(j%3==0 or j%5==0): s1.append(j)
for j in range(1,v): if(j%3==0 or j%5==0): s2.append(j)
print(sum(s1)) print(sum(s2))
Visit my site
import Control.Applicative import Control.Monad import System.IO
sumTo :: Integer -> Integer sumTo 0 = 0 sumTo n = (n * (n + 1)) div 2
multSum :: Integer -> Integer multSum 0 = 0; multSum n = total_sum where three_max = n div 3; five_max = n div 5; fifteen_max = n div 15; three_mult = 3 * (sumTo three_max); five_mult = 5 * (sumTo five_max); fifteen_mult = 15 * (sumTo fifteen_max); total_sum = three_mult + five_mult - fifteen_mult
main :: IO () main = do t_temp <- getLine let t = read t_temp :: Int forM_ [1..t] $ \a0 -> do n_temp <- getLine let n = read n_temp :: Integer let sum = multSum (n - 1) print (sum)
import Control.Applicative import Control.Monad import System.IO
sumTo :: Integer -> Integer sumTo 0 = 0 sumTo n = (n * (n + 1))
div
2multSum :: Integer -> Integer multSum 0 = 0; multSum n = total_sum where three_max = n
div
3; five_max = ndiv
5; fifteen_max = ndiv
15; three_mult = 3 * (sumTo three_max); five_mult = 5 * (sumTo five_max); fifteen_mult = 15 * (sumTo fifteen_max); total_sum = three_mult + five_mult - fifteen_multmain :: IO () main = do t_temp <- getLine let t = read t_temp :: Int forM_ [1..t] $ \a0 -> do n_temp <- getLine let n = read n_temp :: Integer let sum = multSum (n - 1) print (sum)
for(int a0 = 0; a0 < t; a0++){ int n = in.nextInt(); System.out.println(sumOfMultiples(n-1)); }}
private static long sumOfMultiples(int n) { return sumDivisibleBy(n, 3) + sumDivisibleBy(n, 5) - sumDivisibleBy(n, 15); } private static long sumDivisibleBy(int n, int k) { int p = n / k; return k * (long)p * (p + 1) / 2; } }