Plonic numbers are also called rectangular numbers. Plonic numbers are multiples of two consecutive numbers. We will get an array of integers and we can rotate the numbers in any direction a certain number of times to get all combinations. For any combination produced by rotating numbers, if each array element can be converted to a Plonik number, then we will print true, otherwise we will print false.
First, let’s discuss the proton number: The proton number is the product of two consecutive numbers.
Mathematically, if we have an integer x whose next consecutive number is x 1, and let the number k be the product of both of them, this means: k = (x)*(x 1). Some examples of Pronic numbers are:
0 is the product of 0 and 1.
1 is the product of 1 and 2.
6 is the product of 2 and 3.
-> 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, etc.
Suppose we have an array:
{ 21, 65, 227, 204, 2}
Output: Yes
illustrate:
For the zeroth index: 21, after one rotation it can be converted to 12, which is the product of 3 and 4, so it is a Planck number.
For the first index: 65, after one rotation it can be converted to 56, which is the product of 7 and 8 and therefore a Plonic number.
For the second index: 227, it can be converted to 272 after one rotation, which is a pronic number.
Similarly, 204 to 420 and 2 itself is a proton number.
We have seen the code sample, now let’s get into the steps -
First, we will define a function to rotate a given number. An integer will be passed as argument and will be converted to a string.
Using the substring method, we rotate the string to the right, then convert it back to a number again and back.
We will define the pronic function to check if the current number is pronic.
We will find the lower bound of the square root of the current number and multiply it by its consecutive numbers to determine if the current number is a proton.
We will define a function to find the number of digits in the current number by converting it to a string.
In the main function we will iterate through the array and for each element we will rotate it by its length or until we find the Pronic number.
If we find any number after all iterations that is not a pronic number and we cannot convert it to a pronic number, then we will not print yes.
In the following example, we check if all array elements can be converted to Plonik numbers by rotating the numbers. The input and expected output are given below.
Input: Array = [21, 65, 227, 204, 2]
Expected output: Yes
// function to rotate the digits function rotate(num){ // converting integer to string var str = num.toString(); // putting first index value to last str = str.substring(1) + str.substring(0,1); // converting back string to integer num = parseInt(str); return num; } // function to check whether current number if pronic number or not function isPronic(num){ // getting square root of the current number var cur = Math.sqrt(num); // taking floor of cur cur = Math.floor(cur); if(cur*(cur+1) == num) { return true; } else { return false; } } // function to find the length of the current integer function number_length(num){ var str = num.toString() var len = str.length; return len; } // function to check whether array is pronic or not function check(arr){ var len = arr.length; for(var i =0; i<len; i++){ // getting length of the current number var cur = number_length(arr[i]); while(cur--){ if(isPronic(arr[i])){ break; } arr[i] = rotate(arr[i]); } if(isPronic(arr[i]) == false){ return false; } } return true; } var arr = [21, 65, 227, 204, 2] console.log("Array:", JSON.stringify(arr)) if(check(arr)){ console.log("The elements of array can be converted to pronic numbers."); } else{ console.log("The elements of array can't be converted to pronic numbers."); }
Array: [21,65,227,204,2] The elements of array can be converted to pronic numbers.
The time complexity of the above code is O(N), where N is the size of the array. Here we get an extra number-sized logarithmic factor for looping over the array and taking its square root, but since the maximum length of a given integer is very small, there is no impact on linear time complexity.
The space complexity of the above code is constant or O(1) since we are not using any extra space here.
In this tutorial we implemented a JavaScript program to find the weather, we convert each element of the array into a pronic number simply by rotating its number left or right. We defined some functions to rotate numbers, check if they are protons, and get the number of digits. The time complexity of the above code is O(N) and the space complexity is O(1).
The above is the detailed content of JavaScript program checks if all array elements can be converted to pronic numbers by rotating numbers. For more information, please follow other related articles on the PHP Chinese website!