幸运数字-哈沙德数

问题描述

哈沙德数是指在某个固定的进位制当中,可以被各位数字之和整除的正整 数。例如 126 是十进制下的一个哈沙德数,因为 (126) 10 mod (1+2+6) = 0 ; 126 也是八进制下的哈沙德数,因为 (126) 10 = (176) 8 , (126) 10 mod (1 + 7 + 6) = 0 ; 同时 126 也是 16 进制下的哈沙德数,因为 (126) 10 = (7 e ) 16 , (126) 10 mod (7 + e ) = 0 。小蓝认为,如果一个整数在二进制、八进制、十进制、十六进制下均为 哈沙德数,那么这个数字就是幸运数字,第 1 至第 10 个幸运数字的十进制表示 为:1 , 2 , 4 , 6 , 8 , 40 , 48 , 72 , 120 , 126 . . . 。现在他想知道第 2023 个幸运数 字是多少?你只需要告诉小蓝这个整数的十进制表示即可。

 215040

python

def pro1(x, type):
    a = []
    while x > 0:
        help = x % type
        x //= type
        a.append(help)

    # for s in a:
    #     print(s)

    sum_number = 0
    for i in a:
        sum_number += i
    # print(sum_number)
    return sum_number


def pro2(x):
    if x % pro1(x, 2) == 0 and x % pro1(x, 8) == 0 and x % pro1(x, 10) == 0 and x % pro1(x, 16) == 0:
        return True
    return False


if __name__ == '__main__':
    i = 1
    count = 0
    while count < 2023:
        if pro2(i):
            count += 1
            print(count, i)
        i += 1

c++

#include <iostream>
#include <vector>

int pro1(int x, int type) {
    std::vector<int> a;
    while (x > 0) {
        int help = x % type;
        x /= type;
        a.push_back(help);
    }

    int sum_number = 0;
    for (int i : a) {
        sum_number += i;
    }
    return sum_number;
}

bool pro2(int x) {
    if (x % pro1(x, 2) == 0 && x % pro1(x, 8) == 0 && x % pro1(x, 10) == 0 && x % pro1(x, 16) == 0) {
        return true;
    }
    return false;
}

int main() {
    int i = 1;
    int count = 0;
    while (count < 2023) {
        if (pro2(i)) {
            count++;
            std::cout << count << " " << i << std::endl;
        }
        i++;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值