

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding a number, when multiplied with input number yields input number in JavaScript
Problem
We are required to write a JavaScript function that takes in a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p
- we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n.
In other words −
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case, we will return k, if not return -1.
Example
Following is the code −
const num = 695; const p = 2; const findDesiredNumber = (num, p) => { let sum = 0; let str = String(num); for(const char in str){ sum += str[char] * p; p++; }; return Number.isInteger(sum/num) ? sum/num : -1; }; console.log(findDesiredNumber(num, p));
Output
Following is the console output −
-1
- Related Questions & Answers
- Finding the largest 5 digit number within the input number using JavaScript
- Finding all possible prime pairs that sum upto input number using JavaScript
- HTML DOM Input Number Object
- Finding whether a number is triangular number in JavaScript
- Check if input is a number or letter in JavaScript?
- Check whether a series of operations yields a given number with JavaScript Recursion
- Get number from user input and display in console with JavaScript
- HTML DOM Input Number stepUp() Method
- HTML DOM Input Number stepDown() Method
- HTML DOM Input Number form Property
- HTML DOM Input Number autofocus Property
- HTML DOM Input Number disabled Property
- HTML DOM Input Number name Property
- HTML DOM Input Number readOnly Property
- HTML DOM Input Number required Property
Advertisements