![]() |
We have given an input number, and our task is to check if it is a lucky number or not in PHP based on the criteria that all digits in the number must be different. Examples: Input: n = 7 Below are the approaches to check for lucky numbers in PHP: Table of Content Using for LoopIn this approach, we are using a for loop to iterate through numbers from 2 to n−1 to check if n has any divisors other than 1 and itself. If any such divisor is found, the count variable is incremented. Finally, if the count is greater than 0, the number n is deemed not lucky; otherwise, it is considered a lucky number. Example: This example shows the implementation of the above-explained approach.
Output 9 is not a lucky number Using ArrayIn this approach, we are using an array to track removed elements, initializing it with false values representing numbers that are not yet removed. The code iteratively marks multiples of each prime number as removed, finally determining if the input number is lucky based on its presence in the array. Example: This example shows the implementation of the above-explained approach.
Output 9 is not a lucky number |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |