Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmSum of all numbers divisible by 6 in a given range

Sum of all numbers divisible by 6 in a given range

Given a range L-R, find the sum of all numbers divisible by 6 in range L-R
L and R are very large.
Examples: 
 

Input : 1 20
Output : 36 
Explanation: 6 + 12 + 18 = 36 

Input : 5 7  
Output : 6
Explanation: 6 is the only divisible number 
in range 5-7 

 

A naive approach is be to run a loop from L to R and sum up all the numbers divisible by 6.
An efficient approach is to sum all the numbers divisible by 6 up to R in sum, and sum all numbers divisible by 6 up to L-1. And then there subtraction will be the answer. 
sum = 6 + 12 + 8 + …….(R/6)terms. 
sum = 6(1 + 2 + 3……R/6 terms)
 

sumR = 3 * (R/6) * (R/6+1) 

similarly we get 
 

sumL as 3 * ((L-1)/6) * ((L-1/6)+1) 

and the final answer as sumR – sumL.
 

C++




// CPP program to find sum of numbers divisible
// by 6 in a given range.
#include <bits/stdc++.h>
using namespace std;
 
// function to calculate the sum of
// all numbers divisible by 6 in range L-R..
int sum(int L, int R)
{
    // no of multiples of 6 upto r
    int p = R / 6;
 
    // no of multiples of 6 upto l-1
    int q = (L - 1) / 6;
 
    // summation of all multiples of 6 upto r
    int sumR = 3 * (p * (p + 1));
 
    // summation of all multiples of 6 upto l-1
    int sumL = (q * (q + 1)) * 3;
 
    // returns the answer
    return sumR - sumL;
}
 
// driver program to test the above function
int main()
{
    int L = 1, R = 20;
    cout << sum(L, R);
    return 0;
}


Java




// Java program to find sum of numbers
// divisible by 6 in a given range.
import java.io.*;
class GFG {
 
// function to calculate the sum
// of all numbers divisible by 6
// in range L-R..
static int sum(int L, int R)
{
    // no of multiples of 6 upto r
    int p = R / 6;
 
    // no of multiples of 6 upto l-1
    int q = (L - 1) / 6;
 
    // summation of all multiples of
    // 6 upto r
    int sumR = 3 * (p * (p + 1));
 
    // summation of all multiples of
    // 6 upto l-1
    int sumL = (q * (q + 1)) * 3;
 
    // returns the answer
    return sumR - sumL;
}
 
// driver program
public static void main(String[] args)
{
    int L = 1, R = 20;
    System.out.println(sum(L, R));
}
}
 
// This code is contributed by Prerna Saini


Python




# Python3 program to find sum of numbers divisible
# by 6 in a given range.
 
def sumDivisible(L, R):
   
    # no of multiples of 6 upto r
    p = int(R/6)
 
    # no of multiples of 6 upto l-1
    q = int((L-1)/6)
 
    # summation of all multiples of 6 upto r
    sumR = 3 * (p * (p + 1))
 
    # summation of all multiples of 6 upto l-1
    sumL = (q * (q + 1)) * 3
 
    # returns the answer
    return sumR - sumL
 
# driver code
L = 1
R = 20
print(sumDivisible(L,R))
 
# This code is contributed by 'Abhishek Sharma 44'.


C#




// C# program to find sum of numbers
// divisible by 6 in a given range.
using System;
 
class GFG {
     
    // function to calculate the sum
    // of all numbers divisible by 6
    // in range L-R..
    static int sum(int L, int R)
    {
         
        // no of multiples of 6 upto r
        int p = R / 6;
     
        // no of multiples of 6 upto l-1
        int q = (L - 1) / 6;
     
        // summation of all multiples of
        // 6 upto r
        int sumR = 3 * (p * (p + 1));
     
        // summation of all multiples of
        // 6 upto l-1
        int sumL = (q * (q + 1)) * 3;
     
        // returns the answer
        return sumR - sumL;
    }
     
    // driver program
    public static void Main()
    {
        int L = 1, R = 20;
         
        Console.WriteLine(sum(L, R));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP




<?php
// PHP program to find sum of numbers
// divisible by 6 in a given range.
 
// function to calculate the sum of
// all numbers divisible by 6 in range L-R..
function sum($L, $R)
{
     
    // no of multiples of 6 upto r
    $p = intval($R / 6);
 
    // no of multiples of 6 upto l-1
    $q = intval(($L - 1) / 6);
 
    // summation of all multiples
    // of 6 upto r
    $sumR = intval(3 * ($p * ($p + 1)));
 
    // summation of all multiples
    // of 6 upto l-1
    $sumL = intval(($q * ($q + 1)) * 3);
 
    // returns the answer
    return $sumR - $sumL;
}
 
// Driver Code
$L = 1;
$R = 20;
echo sum($L, $R);
 
// This code is contributed by Sam007
?>


Javascript




// Javascript program to find sum of numbers divisible
// by 6 in a given range.
<script>
     
    // function to calculate the sum of
    // all numbers divisible by 6 in range L-R..
    function sum(L, R)
    {
        // no of multiples of 6 upto r
        let p = Math.floor(R / 6);
 
        // no of multiples of 6 upto l-1
        let q = Math.floor((L - 1) / 6);
 
        // summation of all multiples of 6 upto r
        let sumR = Math.floor(3 * (p * (p + 1)));
 
        // summation of all multiples of 6 upto l-1
        let sumL = Math.floor((q * (q + 1)) * 3);
 
        // returns the answer
        return sumR - sumL;
    }
 
    // Driver Code
    let L = 1, R = 20;
    document.write(sum(L, R));
     
    // This code is contributed by ajaykrsharma132.
</script>


Output: 
 

36

Time Complexity: O(1), as we are not using any loop or recursion to traverse.

Auxiliary Space: O(1), as we are not using any extra space.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments