Skip to content
Related Articles

Related Articles

Form a number using corner digits of powers
  • Difficulty Level : Medium
  • Last Updated : 24 Aug, 2020

Given two integers N and X. Make a number in such a way that the number contains the first and last digit occurring in N^1, N^2, N^3,  .... N^X.

Examples :

Input : N = 10, X = 5 
Output : 1010101010
Explanation :
10^1 = 10
10^2 = 100
10^3 = 1000
10^4 = 10000
10^5 = 100000
Take First and Last Digit of each Power to
get required number.

Input : N = 19, X = 4 
Output : 19316911
Explanation :
19^1 = 19
19^2 = 361
19^3 = 6859
19^4 = 130321
Take First and Last Digit of each Power to get required number.

Recommended: Please solve it on “PRACTICE“first, before moving on to the solution.

Approach :
1. Calculate all Powers of N from 1 to X one by one.
2. Store the Output in power[] array.
3. Store power[0] i.e, last_digit and power[power_size – 1] i.e, unit_digit from power[] array to result[] array.
4. Print the result[] Array.



Below is the implementation of the above approach :

C++




// C++ program to find number formed by
// corner digits of powers.
#include <bits/stdc++.h>
using namespace std;
  
// Find next power by multiplying N with
// current power
void nextPower(int N, vector<int> &power)
{
    int carry = 0;
  
    for (int i=0 ; i < power.size(); i++)
    {
        int prod = (power[i] * N) + carry ;
  
        // Store digits of Power one by one.
        power[i] = prod % 10 ;
  
        // Calculate carry.
        carry = prod / 10 ;
    }
  
    while (carry)
    {
        // Store carry in Power array.
        power.push_back(carry % 10);
        carry = carry / 10 ;
    }
}
  
// Prints number formed by corner digits of
// powers of N.
void printPowerNumber(int X, int N)
{
    // Storing N raised to power 0
    vector<int> power;
    power.push_back(1);
  
    // Initializing empty result
    vector<int> res;
  
    // One by one compute next powers and
    // add their corner digits.
    for (int i=1; i<=X; i++)
    {
        // Call Function that store power
        // in Power array.
        nextPower(N, power) ;
  
        // Store unit and last digits of
        // power in res.
        res.push_back(power.back());
        res.push_back(power.front());
    }
  
    for (int i=0 ; i < res.size(); i++)
        cout << res[i] ;
}
  
// Driver Code
int main()
{
    int N = 19 , X = 4;
    printPowerNumber(X, N);
    return 0 ;
}

Java




// Java program to find number formed by
// corner digits of powers.
import java.io.*;
import java.util.*;
   
public class GFG {
      
    static List<Integer> power = new ArrayList<Integer>();
    // Find next power by multiplying N 
    // with current power
    static void nextPower(Integer N)
    {
        Integer carry = 0;
       
        for (int i = 0; i < power.size(); i++)
        {
            Integer prod = (power.get(i) * N) + carry ;
       
            // Store digits of Power one by one.
            power.set(i,prod % 10);
       
            // Calculate carry.
            carry = prod / 10 ;
        }
       
        while (carry >= 1)
        {
            // Store carry in Power array.
            power.add(carry % 10);
            carry = carry / 10 ;
        }
    }
       
    // Prints number formed by corner digits of
    // powers of N.
    static void printPowerNumber(int X, int N)
    {
           
        // Storing N raised to power 0
        power.add(1);
       
        // Initializing empty result
        List<Integer> res = new ArrayList<Integer>();
       
        // One by one compute next powers and
        // add their corner digits.
        for (int i = 1; i <= X; i++)
        {
            // Call Function that store power
            // in Power array.
            nextPower(N) ;
       
            // Store unit and last digits of
            // power in res.
            res.add(power.get(power.size() - 1));
            res.add(power.get(0));
        }
       
        for (int i = 0 ; i < res.size(); i++)
            System.out.print(res.get(i)) ;
    }
       
    // Driver Code
    public static void main(String args[])
    {
        Integer N = 19 , X = 4;
        printPowerNumber(X, N);
    }
}
   
// This code is contributed by Manish Shaw
// (manishshaw1)

Python3




# Python3 program to find 
# number formed by 
# corner digits of powers.
   
# Storing N raised to power 0
power = []
  
# Find next power by multiplying 
# N with current power
def nextPower(N) :
    global power
    carry = 0
    
    for i in range(0, len(power)) :
        prod = (power[i] * N) + carry 
    
        # Store digits of 
        # Power one by one.
        power[i] = prod % 10 
    
        # Calculate carry.
        carry = (int)(prod / 10
    
    while (carry) :
  
        # Store carry in Power array.
        power.append(carry % 10)
        carry = (int)(carry / 10
    
# Prints number formed by corner 
# digits of powers of N.
def printPowerNumber(X, N) :
    global power
    power.append(1)
    
    # Initializing empty result
    res = []
    
    # One by one compute next powers 
    # and add their corner digits.
    for i in range(1, X+1) :
  
        # Call Function that store 
        # power in Power array.
        nextPower(N) 
    
        # Store unit and last 
        # digits of power in res.
        res.append(power[-1])
        res.append(power[0])
    
    for i in range(0, len(res)) :
        print (res[i], end="") 
    
# Driver Code
N = 19 
X = 4
printPowerNumber(X, N)
    
# This code is contributed by 
# Manish Shaw(manishshaw1)

C#




// C# program to find number formed by
// corner digits of powers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
  
class GFG {
      
    // Find next power by multiplying N 
    // with current power
    static void nextPower(int N, ref List<int> power)
    {
        int carry = 0;
      
        for (int i = 0; i < power.Count; i++)
        {
            int prod = (power[i] * N) + carry ;
      
            // Store digits of Power one by one.
            power[i] = prod % 10 ;
      
            // Calculate carry.
            carry = prod / 10 ;
        }
      
        while (carry >= 1)
        {
            // Store carry in Power array.
            power.Add(carry % 10);
            carry = carry / 10 ;
        }
    }
      
    // Prints number formed by corner digits of
    // powers of N.
    static void printPowerNumber(int X, int N)
    {
          
        // Storing N raised to power 0
        List<int> power = new List<int>();
        power.Add(1);
      
        // Initializing empty result
        List<int> res = new List<int>();
      
        // One by one compute next powers and
        // add their corner digits.
        for (int i = 1; i <= X; i++)
        {
            // Call Function that store power
            // in Power array.
            nextPower(N, ref power) ;
      
            // Store unit and last digits of
            // power in res.
            res.Add(power.Last());
            res.Add(power.First());
        }
      
        for (int i = 0 ; i < res.Count; i++)
            Console.Write(res[i]) ;
    }
      
    // Driver Code
    public static void Main()
    {
        int N = 19 , X = 4;
        printPowerNumber(X, N);
    }
}
  
// This code is contributed by Manish Shaw
// (manishshaw1)

PHP




<?php
// PHP program to find 
// number formed by 
// corner digits of powers.
  
// Find next power by multiplying 
// N with current power
function nextPower($N, &$power)
{
    $carry = 0;
  
    for ($i = 0 ; $i < count($power); $i++)
    {
        $prod = ($power[$i] * 
                 $N) + $carry ;
  
        // Store digits of 
        // Power one by one.
        $power[$i] = $prod % 10 ;
  
        // Calculate carry.
        $carry = (int)($prod / 10) ;
    }
  
    while ($carry)
    {
        // Store carry in Power array.
        array_push($power, $carry % 10);
        $carry = (int)($carry / 10) ;
    }
}
  
// Prints number formed by corner 
// digits of powers of N.
function printPowerNumber($X, $N)
{
    // Storing N raised to power 0
    $power = array();
    array_push($power, 1);
  
    // Initializing empty result
    $res = array();
  
    // One by one compute next powers 
    // and add their corner digits.
    for ($i = 1; $i <= $X; $i++)
    {
        // Call Function that store 
        // power in Power array.
        nextPower($N, $power) ;
  
        // Store unit and last 
        // digits of power in res.
        array_push($res
                   $power[count($power) - 1]);
        array_push($res, $power[0]);
    }
  
    for ($i = 0 ; $i < count($res); $i++)
        echo ($res[$i]) ;
  
// Driver Code
$N = 19; $X = 4;
printPowerNumber($X, $N);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>
Output :
19316911

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :