search
HomeWeb Front-endJS TutorialJavaScript program checks if all array elements can be converted to pronic numbers by rotating numbers

JavaScript 程序检查所有数组元素是否可以通过旋转数字转换为 pronic 数

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.

Pronic number

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.

Example

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.

method

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.

Example

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.");
}

Output

Array: [21,65,227,204,2]
The elements of array can be converted to pronic numbers.

Time and space complexity

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 conclusion

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor