Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFind the last digit of given series

Find the last digit of given series

Given an integer n, find the last digit of this sequence,\displaystyle S =\sum_{i=0,\ 2^i\le n}^\infty \sum_{j=0}^n 2^{2^i + 2j}  i.e. Summation of F(n) from i = 0 to 2i ? n, where F(n) is the summation of 22i+2j Where j varies from 0 to n. n can varies from 0 to 1017 Examples:

Input: 2
Output: 6
Explanation:
After computing the above expression, the value
obtained is 216. Hence the last digit is 6.

Input: 3
Output: 0

A Naive approach is to run two loops, one for ‘i’ and other for ‘j’ and compute the value after taking (modulo 10) with every calculated value. But this approach will definitely time out for large value of n. An efficient approach is to expand the above expression in a general form that can easily be calculated. \displaystyle S =\sum_{i=0,\ 2^i\le n}^\infty \sum_{j=0}^n 2^{2^i + 2j}  [Tex]\implies \displaystyle \sum_{i=0,\ 2^i\le n}^\infty \sum_{j=0}^n 2^{2^i}\cdot 2^{2j}  [/Tex]\implies\displaystyle\sum_{i=0,\ 2^i\le n}^\infty 2^{2^i} \cdot \sum_{j=0}^n 2^{2j}  [Tex]\implies\displaystyle\sum_{i=0,\ 2^i\le n}^\infty 2^{2^i} \cdot \sum_{j=0}^n 4^j [/Tex]

  1. First expression \sum_{i=0,\ 2^i\le n}^\infty 2^{2^i}  can be calculated directly by iterating a loop for all values of ‘i’ till 2i ? n.
  2. Second expression \sum_{j=0}^n 4^j  can be calculated easily by using Geometric series formula i.e., \sum_{j=0}^n r^k = \dfrac{r^{n+1} - 1}{r-1}
  3. Final answer will be the product of both these calculated result in both the steps. But after performing any calculation part of these expression, we have to take modulo with 10 to avoid overflow.

C++




// C++ program to calculate to find last
// digit of above expression
#include <bits/stdc++.h>
using namespace std;
   
/* Iterative Function to calculate (x^y)%p in O(log y) */
long long powermod(long long x, long long y, long long p)
{
    long long res = 1; // Initialise result
   
    x = x % p; // Update x if it is more than or
              // equal to p
   
    while (y > 0) {
   
        // If y is odd, multiply x with result
        if (y & 1LL)
            res = (res * x) % p;
   
        // y must be even now
        y = y >> 1LL; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
   
// Returns modulo inverse of a with respect to m
// using extended Euclid Algorithm
long long modInverse(long long a, long long m)
{
    long long m0 = m, t, q;
    long long x0 = 0, x1 = 1;
   
    if (m == 1)
        return 0;
   
    while (a > 1) {
   
        // q is quotient
        q = a / m;
   
        t = m;
   
        // m is remainder now, process same as
        // Euclid's algo
        m = a % m, a = t;
   
        t = x0;
   
        x0 = x1 - q * x0;
   
        x1 = t;
    }
   
    // Make x1 positive
    if (x1 < 0)
        x1 += m0;
   
    return x1;
}
   
// Function to calculate the above expression
long long evaluteExpression(long long& n)
{
    // Initialize the result
    long long firstsum = 0, mod = 10;
   
    // Compute first part of expression
    for (long long i = 2, j = 0; (1LL << j) <= n; i *= i, ++j)
        firstsum = (firstsum + i) % mod;
   
    // Compute second part of expression
    // i.e., ((4^(n+1) - 1) / 3) mod 10
    // Since division of 3 in modulo can't
    // be performed directly therefore we
    // need to find it's modulo Inverse
    long long secondsum = (powermod(4LL, n + 1, mod) - 1) *
                           modInverse(3LL, mod);
   
    return (firstsum * secondsum) % mod;
}
   
// Driver code
int main()
{
    long long n = 3;
    cout << evaluteExpression(n) << endl;
   
    n = 10;
    cout << evaluteExpression(n);
   
    return 0;
}


Java




// Java program to calculate to find last
// digit of above expression
   
class GFG{
/* Iterative Function to calculate (x^y)%p in O(log y) */
static long powermod(long x, long y, long p)
{
    long res = 1; // Initialise result
   
    x = x % p; // Update x if it is more than or
            // equal to p
   
    while (y > 0) {
   
        // If y is odd, multiply x with result
        if ((y & 1L)>0)
            res = (res * x) % p;
   
        // y must be even now
        y = y >> 1L; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
   
// Returns modulo inverse of a with respect to m
// using extended Euclid Algorithm
static long modInverse(long a, long m)
{
    long  m0 = m, t, q;
    long  x0 = 0, x1 = 1;
   
    if (m == 1)
        return 0;
   
    while (a > 1) {
   
        // q is quotient
        q = a / m;
   
        t = m;
   
        // m is remainder now, process same as
        // Euclid's algo
        m = a % m;
        a = t;
   
        t = x0;
   
        x0 = x1 - q * x0;
   
        x1 = t;
    }
   
    // Make x1 positive
    if (x1 < 0)
        x1 += m0;
   
    return x1;
}
   
// Function to calculate the above expression
static long evaluteExpression(long n)
{
    // Initialize the result
    long firstsum = 0, mod = 10;
   
    // Compute first part of expression
    for (long i = 2, j = 0; (1L << j) <= n; i *= i, ++j)
        firstsum = (firstsum + i) % mod;
   
    // Compute second part of expression
    // i.e., ((4^(n+1) - 1) / 3) mod 10
    // Since division of 3 in modulo can't
    // be performed directly therefore we
    // need to find it's modulo Inverse
    long secondsum = (powermod(4L, n + 1, mod) - 1) *
                        modInverse(3L, mod);
   
    return (firstsum * secondsum) % mod;
}
   
// Driver code
public static void main(String[] args)
{
    long n = 3;
    System.out.println(evaluteExpression(n));
   
    n = 10;
    System.out.println(evaluteExpression(n));
   
}
}
// This code is contributed by mits


Python3




# Python3 program to calculate to find last
# digit of above expression
   
# Iterative Function to calculate (x^y)%p in O(log y)
def powermod(x, y, p):
    
    res = 1; # Initialise result
   
    x = x % p; # Update x if it is more than or
            # equal to p
   
    while (y > 0):
   
        # If y is odd, multiply x with result
        if ((y & 1)>0):
            res = (res * x) % p;
   
        # y must be even now
        y = y >> 1; # y = y/2
        x = (x * x) % p;
           
    return res;
   
# Returns modulo inverse of a with respect to m
# using extended Euclid Algorithm
def modInverse(a, m):
    
    m0 = m;
    x0 = 0;
    x1 = 1;
   
    if (m == 1):
        return 0;
   
    while (a > 1):
   
        # q is quotient
        q = int(a / m);
   
        t = m;
   
        # m is remainder now, process same as
        # Euclid's algo
        m = a % m;
        a = t;
   
        t = x0;
   
        x0 = x1 - q * x0;
   
        x1 = t;
   
    # Make x1 positive
    if (x1 < 0):
        x1 += m0;
   
    return x1;
   
# Function to calculate the above expression
def evaluteExpression(n):
    
    # Initialize the result
    firstsum = 0;
    mod = 10;
   
    # Compute first part of expression
    i=2;
    j=0;
    while ((1 << j) <= n):
        firstsum = (firstsum + i) % mod;
        i *= i;
        j+=1;
   
    # Compute second part of expression
    # i.e., ((4^(n+1) - 1) / 3) mod 10
    # Since division of 3 in modulo can't
    # be performed directly therefore we
    # need to find it's modulo Inverse
    secondsum = (powermod(4, n + 1, mod) - 1) * modInverse(3, mod);
   
    return (firstsum * secondsum) % mod;
   
# Driver code
   
n = 3;
print(evaluteExpression(n));
   
n = 10;
print(evaluteExpression(n));
   
# This code is contributed by mits


C#




// C# program to calculate to find last
// digit of above expression
   
class GFG{
/* Iterative Function to calculate (x^y)%p in O(log y) */
static long powermod(long x, long y, long p)
{
    long res = 1; // Initialise result
   
    x = x % p; // Update x if it is more than or
            // equal to p
   
    while (y > 0) {
   
        // If y is odd, multiply x with result
        if ((y & 1)>0)
            res = (res * x) % p;
   
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
   
// Returns modulo inverse of a with respect to m
// using extended Euclid Algorithm
static long modInverse(long a, long m)
{
    long  m0 = m, t, q;
    long  x0 = 0, x1 = 1;
   
    if (m == 1)
        return 0;
   
    while (a > 1) {
   
        // q is quotient
        q = a / m;
   
        t = m;
   
        // m is remainder now, process same as
        // Euclid's algo
        m = a % m;
        a = t;
   
        t = x0;
   
        x0 = x1 - q * x0;
   
        x1 = t;
    }
   
    // Make x1 positive
    if (x1 < 0)
        x1 += m0;
   
    return x1;
}
   
// Function to calculate the above expression
static long evaluteExpression(long n)
{
    // Initialize the result
    long firstsum = 0, mod = 10;
   
    // Compute first part of expression
    for (int i = 2, j = 0; (1 << j) <= n; i *= i, ++j)
        firstsum = (firstsum + i) % mod;
   
    // Compute second part of expression
    // i.e., ((4^(n+1) - 1) / 3) mod 10
    // Since division of 3 in modulo can't
    // be performed directly therefore we
    // need to find it's modulo Inverse
    long secondsum = (powermod(4L, n + 1, mod) - 1) *
                        modInverse(3L, mod);
   
    return (firstsum * secondsum) % mod;
}
   
// Driver code
public static void Main()
{
    long n = 3;
    System.Console.WriteLine(evaluteExpression(n));
   
    n = 10;
    System.Console.WriteLine(evaluteExpression(n));
   
}
}
// This code is contributed by mits


PHP




<?php
// PHP program to calculate to find
// last digit of above expression
   
/* Iterative Function to calculate
   (x^y)%p in O(log y) */
function powermod($x, $y, $p)
{
    $res = 1; // Initialise result
   
    $x = $x % $p; // Update x if it is more
                  // than or equal to p
   
    while ($y > 0)
    {
   
        // If y is odd, multiply
        // x with result
        if (($y & 1) > 0)
            $res = ($res * $x) % $p;
   
        // y must be even now
        $y = $y >> 1; // y = y/2
        $x = ($x * $x) % $p;
    }
    return $res;
}
   
// Returns modulo inverse of a
// with respect to m using
// extended Euclid Algorithm
function modInverse($a, $m)
{
    $m0 = $m;
    $x0 = 0;
    $x1 = 1;
   
    if ($m == 1)
        return 0;
   
    while ($a > 1)
    {
   
        // q is quotient
        $q = (int)($a / $m);
   
        $t = $m;
   
        // m is remainder now, process
        // same as Euclid's algo
        $m = $a % $m;
        $a = $t;
   
        $t = $x0;
   
        $x0 = $x1 - $q * $x0;
   
        $x1 = $t;
    }
   
    // Make x1 positive
    if ($x1 < 0)
        $x1 += $m0;
   
    return $x1;
}
   
// Function to calculate the
// above expression
function evaluteExpression($n)
{
    // Initialize the result
    $firstsum = 0;
    $mod = 10;
   
    // Compute first part of expression
    for ($i = 2, $j = 0; (1 << $j) <= $n;
                          $i *= $i, ++$j)
        $firstsum = ($firstsum + $i) % $mod;
   
    // Compute second part of expression
    // i.e., ((4^(n+1) - 1) / 3) mod 10
    // Since division of 3 in modulo can't
    // be performed directly therefore we
    // need to find it's modulo Inverse
    $secondsum = (powermod(4, $n + 1, $mod) - 1) *
                  modInverse(3, $mod);
   
    return ($firstsum * $secondsum) % $mod;
}
   
// Driver code
$n = 3;
echo evaluteExpression($n) . "\n";
   
$n = 10;
echo evaluteExpression($n);
   
// This code is contributed by mits
?>


Javascript




// JavaScript program to calculate to find last
// digit of above expression
   
/* Iterative Function to calculate (x^y)%p in O(log y) */
function powermod(x, y, p)
{
    let res = 1; // Initialise result
   
    x = x % p; // Update x if it is more than or
            // equal to p
   
    while (y > 0) {
   
        // If y is odd, multiply x with result
        if ((y & 1)>0)
            res = (res * x) % p;
   
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
   
// Returns modulo inverse of a with respect to m
// using extended Euclid Algorithm
function modInverse(a, m)
{
    let  m0 = m, t, q;
    let  x0 = 0, x1 = 1;
   
    if (m == 1)
        return 0;
   
    while (a > 1) {
   
        // q is quotient
        q = Math.floor(a / m);
   
        t = m;
   
        // m is remainder now, process same as
        // Euclid's algo
        m = a % m;
        a = t;
   
        t = x0;
   
        x0 = x1 - q * x0;
   
        x1 = t;
    }
   
    // Make x1 positive
    if (x1 < 0)
        x1 += m0;
   
    return x1;
}
   
// Function to calculate the above expression
function evaluteExpression(n)
{
    // Initialize the result
    let firstsum = 0, mod = 10;
   
    // Compute first part of expression
    for (var i = 2, j = 0; (1 << j) <= n; i *= i, ++j)
        firstsum = (firstsum + i) % mod;
   
    // Compute second part of expression
    // i.e., ((4^(n+1) - 1) / 3) mod 10
    // Since division of 3 in modulo can't
    // be performed directly therefore we
    // need to find it's modulo Inverse
    let secondsum = (powermod(4, n + 1, mod) - 1) *
                        modInverse(3, mod);
   
    return (firstsum * secondsum) % mod;
}
   
// Driver code
let n = 3;
console.log(evaluteExpression(n));
   
n = 10;
console.log(evaluteExpression(n));
   
 
// This code is contributed by phasing17


Output:

0
8

Time complexity: O(log(n))
Auxiliary space: O(1)

Note: Asked in TCS Code vita contest.

If you like neveropen and would like to contribute, you can also write an article using contribute.neveropen.co.uk or mail your article to contribute@neveropen.co.uk. See your article appearing on the neveropen main page and help other Geeks.

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!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments