问题描述
哈沙德数是指在某个固定的进位制当中,可以被各位数字之和整除的正整 数。例如 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;
}