Form a Number Using Corner Digits of Powers


What are Corner digits?

The corner digits of a number refer to the rightmost and the leftmost digits.

For example, the corner digits of 1234 are 1 and 4.

The corner digits of a single-digit number will be the number twice.

For example, the corner digits of 2 will be 2 and 2.

Problem Statement

For given two numbers, n, and x, form a number using the corner digits of all the powers of n from 1 and x, i.e., n1, n2....nx.

Examples

Input: n = 2, x = 4
Output: 22448816

Explanation

21 = 2. Corner digits = 2, 2.
22 = 4. Corner digits = 4, 4.
23 = 8. Corner digits = 8, 8.
24 = 16. Corner digits = 1, 6.

Hence, the number formed with the corner digits of all the numbers above is 22448816.

Input: n = 16, x = 5
Output: 1626466616

Explanation

161 = 16. Corner digits = 1, 6.
162 = 256. Corner digits = 2, 6.
163 = 4096. Corner digits = 4, 6.
164 = 65536. Corner digits = 6, 6.
165 = 1048576. Corner digits = 1, 6.

Hence, the number formed with the corner digits of all the numbers above is 1626466616.

Algorithm

  • Find out all the powers of N from 1 to X one by one.

  • Store the powers in the power array.

  • Create an answer array for storing the final output.

  • Store the first and last numbers of the power array in the answer array.

  • Print the answer array.

Pseudocode

Function main() −

  • Take input of n, and x from the user.

  • Function Call corner_digits_number().

Function corner_digits_number(int n, int x) −

  • Create vector power, and answer, to store the powers and the final answer respectively.

  • Store 1 in power. (n raised to power 0).

  • For i=1 to i=x −

    • Function call store_next_power()

    • Store the last digit of power in the answer.

    • Store the first digit of power in the answer.

  • Function call print_output().

Function store_next_power(vector<int>&power, int n) −

  • Initialize carry = 0, product = 1.

  • For i=0 to i=power.size()-1 −

    • Product -> power[i] * n + carry.

    • power[i] -> product %10.

    • carry -> product/10.

  • while carry is not equal to zero −

    • Store carry % 10 in power.

    • carry -> carry /10.

Function print_output(vector<int>answer) −

  • For i=0 to i=answer.size()-1 −

    • Print answer[i].

Example

Below is a C++ program to form a number from the corner digits of all the powers of n from 1 to x.

#include<iostream>
#include<vector>
using namespace std;
 
//This function calculates the next power value and stores
// it in the power vector
void store_next_power(vector<int>&power, int n){
   int carry = 0, product;
   for(int i=0 ; i < power.size() ; i++){
      
      //Calculating the power of n
      product = ( power[i] * n ) + carry;
      
      //store the digit of power for the particular index
      power[i] = product % 10 ;
      
      //the carry will be added to the next index's value.
      carry = product / 10 ;
   }
   
   //The carry will also be added to the power vector.
   while(carry){
      power.push_back(carry % 10);
      carry = carry / 10 ;
   }
}

//Print the final output
void print_output(vector<int>v){
   for(int i=0 ; i < v.size() ; i++){
      cout<<v[i]<<", ";
   }
}

//This function prints the number formed by the corner digits
// of all the powers of n from 1 to x.
void corner_digits_number(int n, int x){

   //vector to store the powers
   vector<int>power;
   
   //Store n raised to the power 0.
   //This will be useful in finding the next power of n.
   power.push_back(1);
   
   //vector to store the final output
   vector<int>answer;
   
   //Calculate all the powers of n from 1 and x
   for(int i=0 ; i < x ; i++){
   
      //Function call to store the next power value
      //in the vector
      store_next_power(power,n);
      
      //add the first and last digits in the power vector
      //to the answer vector
      // The last digit in the power is first pushed to the answer vector 
      // because it contains the unit place value.
      answer.push_back(power.back());
      answer.push_back(power.front());
   }
   
   //Function call to print the final number.
   print_output(answer);
}
int main(){
   int n = 6, x = 4;
   //Function call to print the required number
   cout<< "Corner digits for n=6 and x =4 are: "<<endl;
   corner_digits_number(n,x);
   return 0;
}

Output

Corner digits for n=6 and x =4 are: 
6, 6, 3, 6, 2, 6, 1, 6,

This article discusses the problem of forming a number using the corner digits of all the powers of n from 1 to x. n and x are two given numbers in the problem.

The problem is first explained with a few examples, and then the approach is discussed.

The algorithm, pseudocode, and C++ program are given in the article. Time and space complexities are also mentioned.

Updated on: 10-Mar-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements