题目52:找出最小的正整数x,使得2x, 3x, 4x, 5x和6x都包含同样的数字。
Permuted multiplesIt can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
题目:
125874 和它的二倍,251748, 包含着同样的数字,只是顺序不同。
找出最小的正整数 x,使得 2x, 3x, 4x, 5x, 和 6x 都包含同样的数字。
都有这么多啦~ 142875
2*142857=285714
3*142857=428571
4*142857=571428
......
#include <iostream>
#include <string>
#include <algorithm>
using namespacestd;;
bool xiangtong(int a,int b)
{
char bufa;
char bufb;
itoa(a,bufa,10);
itoa(b,bufb,10);
string stra(bufa),strb(bufb);
sort(stra.begin(),stra.end());
sort(strb.begin(),strb.end());
return stra==strb;
}
int main()
{
//std::cout<<xiangtong(142875,3*142875);
for (int n =10;n<=1000000;n++)
{
if(xiangtong(2*n,n) &&xiangtong(3*n,n)&&xiangtong(4*n,n)&&xiangtong(5*n,n)&&xiangtong(6*n,n))
{
cout<<n<<endl;
}
}
return 0;
} 142857
for i in range(100000,200000):
x1 = sorted(list(str(i)))
x2 = sorted(list(str(i*2)))
x3 = sorted(list(str(i*3)))
x4 = sorted(list(str(i*4)))
x5 = sorted(list(str(i*5)))
x6 = sorted(list(str(i*6)))
if x1 == x2 == x3 == x4 == x5 == x6:
print (i)
exit() i = 100
while True:
if len(str(i))==len(str(2*i))==len(str(3*i))==len(str(5*i))==len(str(6*i)):
if set(str(i))==set(str(2*i))==set(str(3*i))==set(str(5*i))==set(str(6*i)):
print(i)
break
i+=1
结果:142857 # encoding:utf-8
# 1x 2x 3x 4x 5x 6x数字相同
from time import time
def euler052(N=1000000):
for i in range(100000, 170000):
if set(str(i)) == set(str(2 * i)) == set(str(3 * i)) == set(str(4 * i)) == set(str(5 * i)) == set(str(6 * i)):
print(i)
return
if __name__ == '__main__':
start = time()
euler052()
print('cost %.6f sec' % (time() - start)) 本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:30 编辑
#!/usr/bin/env python
#coding:utf-8
def Ss():
n=1
while True:
n+=1
B=0
for i in list(range(3,7)):
if sorted(list(str(n*i))) ==sorted(list(str(n*2))):
B+=1
if B==4:
return n
if __name__ == '__main__':
num=Ss()
print(num) 此代码使用matlab编程
Problem52所用时间为: 18.6882秒
Problem52的答案为: 142857
%% Problem52.m
% 最后编辑时间:17-06-14 22:34
% 若正整数x,满足2x,3x,4x,5x,6x均为排列,找到满足此条件的最小数
% Problem52所用时间为: 42.5714秒
% Problem52的答案为: 142857
function Output = Problem52()
tic
Start = 100000;
Judge = 0;
while (Judge == 0)
Start = Start + 1;
Judge = PermMul(Start);
end
Output = Start;
toc
disp('此代码使用matlab编程')
disp(['Problem52所用时间为: ',num2str(toc),'秒'])
disp(['Problem52的答案为: ',num2str(Output)])
end
%输入一个数x,检验其2x,3x,4x,5x,6x,是否为排列
%是Judge = 1.不是Judge = 0;
function Judge = PermMul(x)
if nargin == 0
x = 4543;
end
if length(num2str(x)) ~= length(num2str(x*6))
Judge = 0;
else
Stand = num2str(x);
Judge = 1;
for ii = 2:6
if strcmp(sort(num2str(x*ii)),sort(Stand)) == 0
Judge = 0;
break
end
end
end
end 用的matlab
结果是
142857
时间已过 2.864496 秒。
>> 本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:30 编辑
import time
def time_it():
start=time.time()
fun()
end=time.time()
print('cost %.6f Secs'%(end-start))
def fun():
x=1
while True:
x1=set(str(x))
x2=set(str(x * 2))
x3=set(str(x * 3))
x4=set(str(x * 4))
x5=set(str(x * 5))
x6=set(str(x * 6))
if x1 == x2 == x3 == x4 == x5 == x6:
print('%d是最小的正整数 x,使得 2x, 3x, 4x, 5x, 和 6x 都包含同样的数字'%x)
print('%d * 2 = %d'%(x,x*2))
print('%d * 3 = %d'%(x,x*3))
print('%d * 4 = %d'%(x,x*4))
print('%d * 5 = %d'%(x,x*5))
print('%d * 6 = %d'%(x,x*6))
break
else:
x+=1
time_it()
'''
142857是最小的正整数 x,使得 2x, 3x, 4x, 5x, 和 6x 都包含同样的数字
142857 * 2 = 285714
142857 * 3 = 428571
142857 * 4 = 571428
142857 * 5 = 714285
142857 * 6 = 857142
cost 0.596410 Secs
''' 142857
def numList(num):
NumStr = str(num)
temp = []
for i in range(len((NumStr))):
temp.append(int(str(num)))
NumList = list(set(temp))
return NumList
for i in range(1000000):
a = i
b = i*2
c = i*3
d = i*4
e = i*5
f = i*6
if len(str(a)) == len(str(b)) == len(str(c))== len(str(d))==len(str(e)) ==len(str(f)):
if numList(a) == numList(b) == numList(c) == numList(d) == numList(e) == numList(f):
print(i)
结果是:142857
用时:0.624004 秒import time
def cal_result():
number = 100
while True:
mark = 1
for i in range(1, 7):
if set(str(number)) != set(str(number*i)):
mark = 0
break
if mark:
return number
number += 1
print("结果是:{}\n用时:{} 秒".format(cal_result(), time.process_time())) 本帖最后由 永恒的蓝色梦想 于 2020-4-21 14:08 编辑
还是 C++ 牛批
用时 88ms#include<iostream>
using namespace std;
bool judge(int i) {
int j, idx = 6, arr = { 0 };
while (idx) {
j = i * idx;
idx--;
while (j) {
arr++;
j /= 10;
}
}
while (idx < 10u) {
if (arr != arr\
|| arr != arr\
|| arr != arr\
|| arr != arr\
|| arr != arr) {
return true;
}
idx++;
}
return false;
}
int main() {
int i;
for (i = 1; judge(i); i++);
cout << i << endl;
return 0;
} 142857
Process returned 0 (0x0) execution time : 0.043 s
Press any key to continue.
对5个数依次两两判断即可
#include<iostream>
using namespace std;
bool same_digit(int x,int y){
int digit = {0};
while(x){
digit++;
x /= 10;
}
while(y){
digit--;
y /= 10;
}
for (int i = 0;i < 10;i++)
if (digit) return false;
return true;
}
bool judge(int x){
int t = x * 2;
for (int i = 3;i <= 6;i++){
int u = x * i;
if (!same_digit(t,u)) return false;
}
return true;
}
int main(){
for (int i = 1;;i++){
if (judge(i)) {cout << i << endl; break;}
}
return 0;
}
#include <stdio.h>
main()
{
int i = 1, j, k, m, flag = 1;
while (1)
{
i++;
j = i;
int a = { 0 };//标记法
while (j)//将i各个位数用数组a标记
{
k = j % 10;
a = 1;
j /= 10;
}
for (m = 2; m < 7; m++)
{
j = i * m;
while (j)
{
k = j % 10;
if (a != 1)//若数组中没有被标记,直接下一轮循环
{
flag = 0;
goto Label;
}
j /= 10;
}
}
if (flag)
{
printf("%d", i);
break;
}
Label:flag = 1;
}
}
142857 #找出最小的正整数x,使得2x, 3x, 4x, 5x和6x都包含同样的数字
from time import *
'''
一个正整数的2倍如果包含同样数字表示位数应该相同
所以此正整数的范围在10-50,100-500,1000-5000...之内
否则2倍位数不一样
此为基本条件
'''
#把数字转化为列表排序后比较
def transform(num):
num_list = []
for i in str(num):
num_list.append(i)
num_list.sort()
return num_list
#判断是否包含相同数字
def is_same(num, n):
if transform(num) == transform(n * num):
return True
start = time()
a = 10
b = 50
while True:
for x in range(100000, 500000):
if is_same(x, 2) and is_same(x, 3) and is_same(x, 4) and\
is_same(x, 5) and is_same(x, 6):
print(x)
break
break
a *= 10
b *= 10
end = time()
print("用时%.4f秒" % (end-start))
#'''
142857
用时0.1313秒 本帖最后由 guosl 于 2022-1-9 08:56 编辑
/*
欧拉问题52
答案:142857
耗时:0.0143542秒(单核)
0.0026904秒(六核)
*/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <omp.h>
using namespace std;
const int nSteps = 100;
int main(void)
{
int k = 10;
int nMin = 0x7fffffff;
volatile int nContinue = 0;
#pragma omp parallel shared(nContinue) reduction(min:nMin)
while (nContinue == 0)
{
int k1 = 0;
#pragma omp critical
{
k1 = k;
k += nSteps;
}
for (int j = k1; nContinue == 0 && j < k1 + nSteps; ++j)
{
char str, str1;
_itoa_s(j, str, 10);
sort(str, str + strlen(str));
bool bFind = true;
for (int i = 2; i < 7; ++i)
{
int x = i * j;
_itoa_s(x, str1, 10);
sort(str1, str1 + strlen(str1));
if (strcmp(str, str1) != 0)
{
bFind = false;
break;
}
}
if (bFind)
{
if (j < nMin)
{
nMin = j;
#pragma omp atomic
++nContinue;
}
}
}
}
cout << nMin << endl;
return 0;
}
页:
[1]