- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 special type of numbers - JavaScript
In the decimal number system, all the real numbers can be divided into two groups −
- Rational Numbers
- Irrational Numbers
For the scope of this problem we will only discuss the rational numbers,
All those numbers which can be written in the p/q (where q !== 0) form are called rational numbers.
Like 14, 4.6, 3.33333... and many more
The rational numbers, further can be divided into two groups −
- Terminating decimal numbers
- Repeating decimal numbers
This categorization is made on the basis of result obtained upon dividing p by q.
The thumb for this categorization is that −
- We will obtain a terminating decimal number if and only if the prime factors of q are only 2 and 5
We will obtain a repeating decimal number of the prime factors of q includes any other number than 2 or 5.
So, we are required to write a JavaScript function that takes in a number that represents the value q. Our function should return true if for that value of we can obtain a terminating decimal number, false otherwise.
Example
Following is the code −
const num = 1250; const isTerminating = num => { while(num !== 1){ if(num % 2 === 0){ num /= 2; }else if(num % 5 === 0){ num /= 5; }else{ return false; }; }; return true; }; console.log(isTerminating(num));
Output
This will produce the following output in console −
true