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.
defmult_3(num):#byapplyingtheformulaofsumoffirstntermsN=(num-1)//3 # in A.P ,where N -> no. of terms in A.P l=(N*3)#l->lastterma=3#a->firsttermreturn(N*(a+l))//2 #S -> Sum i.e, (N*(a+l))//2defmult_5(num):N=(num-1)//5l=N*5a=5return(N*(a+l))//2defmult_15(num):N=(num-1)//15l=N*15a=15return(N*(a+l))//2t=int(input().strip())fora0inrange(t):n=int(input().strip())ifn<3:print(0)else:print((mult_3(n)+mult_5(n))-mult_15(n))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #1: Multiples of 3 and 5
You are viewing a single comment's thread. Return to all comments →