Find the largest palindrome made from the product of two 3-digit numbers - Javascript

前端 未结 21 1440
感情败类
感情败类 2020-12-28 17:16

Can anyone tell me what\'s wrong with the code. Find the largest palindrome made from the product of two 3-digit numbers.

function largestPalind         


        
21条回答
  •  感情败类
    2020-12-28 17:41

    Swift 3:

    // my approach is to make 6-digit palindrome first and then 
    // check if I can divide it by 3-digit number 
    // (you can see some visual listing at the end of the code)
    // execution time on my laptop is around: 2.75409698486328 sec
    
    import Foundation
    
    func maxPalindrom() -> Int {
    
        var result = 999999
        var i = 9
        var j = 9
        var k = 9
    
        while true {
            while true {
                while true {
                    print("in K loop: \(result) k = \(k)")
    
                    if isDivisible(number: result) {
                        return result
                    }
                    if k <= 0 {
                        k = 9
                        result += 9900
                        break
                    }
                    result -= 1100
                    k -= 1
                }
    
                print("in J loop: \(result)")
                if isDivisible(number: result) {
                    return result
                }
                if j < 0 {
                    j = 9
                    result += 90090
                    break
                }
                result -= 10010
                j -= 1
            }
    
            print("in I loop: \(result)")
            if isDivisible(number: result) {
                return result
            }
            if i < 0 {
                break
            }
            result -= 100001
            i -= 1
        }
    
        if result == 100001 {
            return -1
        }
    
        return -1
    }
    
    
    func isDivisible(number: Int) -> Bool {
        var i = 999
    
        while true {
    
            if number % i == 0 && number / i < 999 {
                return true
            }
    
            if i < 500 {
                return false
            }
    
            i -= 1
        }
    
    }
    
    
    let start = NSDate()
    print(maxPalindrom())       // 906609
    let end = NSDate()
    
    print("executio time: \(end.timeIntervalSince(start as Date)) sec") // ~ execution time: 2.75409698486328 sec
    
    //in K loop: 999999 k = 9
    //in K loop: 998899 k = 8
    //in K loop: 997799 k = 7
    //in K loop: 996699 k = 6
    //in K loop: 995599 k = 5
    //in K loop: 994499 k = 4
    //in K loop: 993399 k = 3
    //in K loop: 992299 k = 2
    //in K loop: 991199 k = 1
    //in K loop: 990099 k = 0
    //in J loop: 999999
    //in K loop: 989989 k = 9
    //in K loop: 988889 k = 8
    //in K loop: 987789 k = 7
    //in K loop: 986689 k = 6
    //in K loop: 985589 k = 5
    //in K loop: 984489 k = 4
    //in K loop: 983389 k = 3
    .....
    

提交回复
热议问题