B1tetheDust
发表于 2022-3-16 16:16:53
import time
def is_palindrome(num):
str_num = str(num)
if str_num == str_num[::-1]:
return True
return False
start = time.perf_counter()
print(max())
end = time.perf_counter()
print('It takes %f s' % (end - start))
清风揽月shine
发表于 2022-5-21 15:19:44
import time
def palindrome_data(power):
"""
: 由几位数构成的回文
:param power:
:return:
"""
st = time.time()
start = 10 ** (power - 1)
end = 10 ** power
all_palin_data = []
t = 0
x = 0
y = 0
for i in range(end, start, -1):# 这里反着取效率最高
for j in range(end, start, -1):
result = i * j
if str(result) == str(result)[::-1]:
if result > t:
t = result # 保存变量占用内存就会浪费时间
x = i
y = j
ele = (x, y, t)
if ele not in all_palin_data:
all_palin_data.append((x, y, t))
break# 这里取到一个最大的就不用继续在这个循环里面浪费时间了
print(all_palin_data)
print('用时%.5f' % (time.time() - st))
开飞机的羊驼
发表于 2022-6-28 17:40:59
num = 13195
list = []
list1 = []
#先用for循环寻找所有的因子并存储list
for i in range(1, num):
if num % i == 0:
list.append(i)
print(list)
# 用两个for循环来判断list中的因子那些是质数
for j in list:
if (j != 1) and (j != num):
for k in range(2, j):
if j % k == 0 :
break
else:
list1.append(j)
print(list1)
print("最大质数因子为%d"%list1[-1])
Asss-whom
发表于 2022-8-9 11:07:59
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
let num = (100..1000)
.filter_map(|x| {
(100..1000)
.map(|y| x * y)
.filter(|x| x.to_string().chars().rev().collect::<String>() == x.to_string())
.max()
})
.max();
println!("{:?}", num);
Ok(())
}
Some(906609)
_乏趣味
发表于 2023-2-10 19:39:13
z=1
for m in range(999999,1,-1):
if str(m)==str(m)[::-1] and z:
for k in range(int(m**0.5),1,-1):
if 99<k<1000 and 99<m/k<1000 and m%k==0:
re=
z=0
print(re)
Eca
发表于 2023-4-6 18:17:33
#include <stdio.h>
int cmp(int n) {
int ax = n / 1000;
int tmpa = 0, tmpb = 0, max = 100;
while (ax > 0) {
tmpa += (ax % 10) * max;
max /= 10;
ax /= 10;
}
max = 100;
ax = tmpa;
while (ax > 0) {
tmpb += (ax % 10) * max;
max /= 10;
ax /= 10;
}
if (tmpb * 1000 + tmpa == n)
return 1;
else
return 0;
}
int main() {
for (int i = 999999;; i--) {
if (cmp(i)) {
for (int j = 999;; j--) {
int t1 = i / j;
float t2 = (float)i / (float)j;
if ((float)t1 == t2 && t1 < 1000 && t2 < 1000 ) {
printf("%d\n", i);
return 0;
} else if (t1 > 1000 || t2 > 1000)
break;
else
continue;
}
} else
continue;
}
return 0;
}
qingyunvi
发表于 2023-10-28 16:48:55
import time
start = time.time()
muli = 0
num_dict = {}
for i in range(100, 1000):
for j in range(100, 1000):
muli = i * j
if str(muli) == str(muli)[::-1]:
output = "%s = %d * %d" % (muli, i, j)
num_dict.setdefault(muli, output)
# print("%s = %d * %d" % (muli, i, j))
keys = num_dict.keys()
max_key = max(keys)
print(num_dict)
end = time.time()
print("time: %s" % (end - start))
906609 = 913 * 993
time: 0.5153927803039551
感觉速度算是比较慢的,应该是嵌套了循环有关
Ian_Li
发表于 2024-9-13 11:36:56
无名侠 发表于 2015-7-9 14:41
难点应该就是判断是否为回文了。
我用的方法有些歪门邪道,利用字符串来判断,不过效率还行。
然后就是三 ...
为什么 第一层循环从500向下递减?也应该从999开始呀
Ian_Li
发表于 2024-9-13 12:09:23
c++练习,day4
i:993,j:913,product:906609
#include <iostream>
using namespace std;
// 翻转数字比较
int isPalindrome(int n) {
int t = n;
int rn = 0;
while (t != 0) {
rn = 10 * rn + t % 10;
t /= 10;
}
if (rn != n) return 0;
return 1;
}
// 转字符串比对字符
int isPalindrome_2(int n) {
string sn = to_string(n);
int len = sn.length();
for (int i = 0; i < len / 2; i++) {
if (sn != sn) return 0;
}
return 1;
}
int main() {
int max_j = 0;
int max_product = 0;
int ir;
int jr;
for (int i = 999; i > 99; i--) {
if (i == max_j) break; // 剪枝
for (int j = i; j > 99; j--) {
int product = i * j;
if (isPalindrome_2(product)) {
if (j > max_j) max_j = j;
if (product > max_product) {
max_product = product;
ir = i;
jr = j;
break;
}
}
}
}
cout << "i:" << ir << ",j:" << jr << ",product:" << max_product << endl;
}