Algorithm


Problem Name: beecrowd | 2867

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2867

Digits

 

By Ricardo Martins, IFSULDEMINAS BR Brazil

Timelimit: 1

Given two integers, n and m, how many digits have nm?

Examples:

2 and 10 - 210 = 1024 - 4 digits

3 and 9 - 39 = 19683 - 5 digits

 

Input

 

The input is composed of several test cases. The first line has an integer C, representing the number of test cases. The following C lines contain two integers N and M (1 <= N, M <= 100).

 

Output

 

For each input test case of your program, you must print an integer containing the number of digits of the result of the calculated power in the respective test case.

 

 

 

Input Sample Output Sample

4
1 1
2 10
3 9
100 100

1
4
5
201

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double t, n, m, x;
    cin >> t;
    for (int i = 0; i  <  t; ++i)
    {
        cin >> n >> m;
        x=pow(n, m);
        x=m*log10(n)+1;
        cout << floor(x) << endl;
    }

    return 0;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
4 1 1 2 10 3 9 100 100

Output

x
+
cmd
1 4 5 201

#2 Code Example with Java Programming

Code - Java Programming


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner leitor = new Scanner(System.in);
        int C = leitor.nextInt();
        for (int i = 0; i  <  C; i++) {
        	int N = leitor.nextInt();
        	int M = leitor.nextInt();
        	int digitos = (int) (Math.log10(Math.pow(N, M)) + 1);
        	System.out.println(digitos);
        }
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
4 1 1 2 10 3 9 100 100

Output

x
+
cmd
1 4 5 201

#3 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const [[numCases], ...lines] = readFileSync("/dev/stdin", "utf8")
	.split("\n")
	.map((line) => line.split(" "))

function main() {
	const responses = lines
		.slice(0, +numCases)
		.map(([N, M]) => {
			return Math.floor(Number.parseInt(M, 10) * Math.log10(Number.parseInt(N, 10))) + 1
		})

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 1 1 2 10 3 9 100 100

Output

x
+
cmd
1 4 5 201

#4 Code Example with Python Programming

Code - Python Programming


c = int(input())
while c:
    c -= 1
    n, m = [int(x) for x in input().split()]
    n = str(n ** m)
    print(n)
    print(len(n))

Copy The Code & Try With Live Editor

Input

x
+
cmd
4 1 1 2 10 3 9 100 100

Output

x
+
cmd
1 4 5 201
Advertisements

Demonstration


Previous
#2863 Beecrowd Online Judge Solution 2863 Umil Bolt Solution in C, C++, Java, Js and Python
Next
#2879 Beecrowd Online Judge Solution 2879 Desvendando Monty Hall Solution in C, C++, Java, Js and Python