Perfect number, divisor

Perfect number

For a positive integer, if it is equal to the sum of all positive factors except itself, we call it "perfect number".

Given an integer n, if it is perfect, return True or False

Example:

Input: 28
Output: True
Interpretation: 28 = 1 + 2 + 4 + 7 + 14

Tips:

The number n entered will not exceed 100,000,000. (1e8)

class Solution {
    public boolean checkPerfectNumber(int num) {
        /*
        1.An odd number is certainly not a perfect number.
        2.For the number 28, you can judge the divisible number in the range of 2-28/2 and add it up to see if it is equal to 28.
        The above method is redundant in judging one by one, because 28% 2 = 0, 28% 14 = 0, judging twice, we can reduce it to one judgment.
        Specifically, when we know 28% 2 = 0, we save 28/2 = 14 results, so that we don't need to judge whether 14 can be excluded.
         for(int i = 2; i < num / i; i++) When i=2, I < 28/2 = 14, which excludes 14
         In this way, each time the range of intervals is reduced to i~num/i, time O(logn)
        */
        if(num % 2 != 0) return false;
        
        int tmp = 1;
        for(int i = 2; i < num / i; i++){
            if(num % i == 0){
                tmp += i + num / i;
            }
        }
        
        return tmp == num;
    }
}

//Author: hjs-5
//Link: https://leetcode-cn.com/problems/perfect-number/solution/javati-jie-by-hjs-5/
//Source: LeetCode
//Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

A divisor is a number that can be divided by every number it contains.

For example, 128 is an autodivisor because 128% 1 = 0, 128% 2 = 0, 128% 8 = 0.

Also, divisors are not allowed to contain 0.

Given the number of upper and lower boundaries, a list is output. The elements of the list are all divisors within the boundaries (including boundaries).

Example 1:

Input:
Upper boundary left = 1, lower boundary right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
Be careful:

The boundary of each input parameter satisfies 1 <= left <= right <== 10000.

class Solution {
    public List<Integer> selfDividingNumbers(int left, int right) {
        /*
        Each bit in num is removed in turn, and then the divisor is determined:
        1.If it's 0, it's certainly not a divisor, because you can't divide it by 0.
        2.If it's not zero, then use the original number pair to modulus the last number just taken down.
        */
        List<Integer> res = new ArrayList<>();
        
        int len = right - left + 1;
        int tmp= left;
        for(int i = left; i < right + 1; i++){
            if(isDividingNumber(i)){
                res.add(i);    
            }
        }
        
        return res;
        
    }
    
    public boolean isDividingNumber(int num){
        int tmp = 0;
        int n = num;
        while(num > 0){
            tmp = num % 10;
            
            if(tmp == 0 || n % tmp != 0){
            
                return false;
            }
            num /= 10;
        }
        return true;
    
    }
}

Posted by shadow-x on Mon, 30 Sep 2019 16:21:15 -0700