Guidelines
- Java
- JavaScript
- Python
- C++
int numberOfDigits = (int) Math.floor(Math.log10(x));
int power = (int) Math.pow(10,numberOfDigits);
// reversing the number and checking overflow
while(x != 0){
int rem = x % 10;
x /= 10;
if (num > Integer.MAX_VALUE/10 || (num == Integer.MAX_VALUE / 10 && rem > 7)) return 0;
num = num * 10 + rem;
}
// if digit is presented with a char
num = (num * 10) + (c - '0');
let numberOfDigits = Math.floor(Math.log10(x));
let power = 10 ** numberOfDigits;
// Reversing the number and checking overflow
let num = 0;
while (x !== 0) {
let rem = x % 10;
x = Math.floor(x / 10);
if (
num > Math.floor(Number.MAX_SAFE_INTEGER / 10) ||
(num === Math.floor(Number.MAX_SAFE_INTEGER / 10) && rem > 7)
) {
return 0;
}
num = num * 10 + rem;
}
// If digit is presented with a char
num = num * 10 + (c - "0");
import math
number_of_digits = int(math.floor(math.log10(x)))
power = 10 ** number_of_digits
# Reversing the number and checking overflow
num = 0
while x != 0:
rem = x % 10
x //= 10
if num > (2 ** 31 - 1) // 10 or (num == (2 ** 31 - 1) // 10 and rem > 7):
return 0
num = num * 10 + rem
# If digit is presented with a char
num = num * 10 + (ord(c) - ord('0'))
#include <cmath>
int number_of_digits = static_cast<int>(std::floor(std::log10(x)));
int power = static_cast<int>(std::pow(10, number_of_digits));
// Reversing the number and checking overflow
int num = 0;
while (x != 0) {
int rem = x % 10;
x /= 10;
if (num > INT_MAX / 10 || (num == INT_MAX / 10 && rem > 7)) {
return 0;
}
num = num * 10 + rem;
}
// If digit is presented with a char
num = num * 10 + (c - '0');
we can formulate recursive relation for pow
operation pow(x, n) = pow(x^n/2) * pow(x^n/2)
for even n
(e.g. x^4 = x^2 * x^2
), while for odd n
we have the following pow(x, n) = pow(x^n/2) * pow(x^n/2) * x
(e.g. x^5 = x^2 * x^2 * x
).
- Java
- JavaScript
- Python
- C++
// get random element from the list
Random rnd = new Random();
list.get(rnd.nextInt(list.size()));
let rnd = Math.floor(Math.random() * list.length);
list[rnd];
import random
rnd = random.randint(0, len(list) - 1)
list[rnd]
#include <random>
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> uni(0, list.size() - 1);
list[uni(rng)];