鱼C论坛

 找回密码
 立即注册
查看: 3310|回复: 16

题目52:找出最小的正整数x,使得2x, 3x, 4x, 5x和6x都包含同样的数字。

[复制链接]
发表于 2015-5-29 23:05:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Permuted multiples

It 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 都包含同样的数字。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-5-30 02:17:40 | 显示全部楼层
都有这么多啦~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-30 23:39:13 | 显示全部楼层
142875
2*142857=285714
3*142857=428571
4*142857=571428
......
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace  std;;
  5. bool xiangtong(int a,int b)
  6. {
  7.         char bufa[20];
  8.         char bufb[20];
  9.         itoa(a,bufa,10);
  10.         itoa(b,bufb,10);
  11.         string stra(bufa),strb(bufb);
  12.         sort(stra.begin(),stra.end());
  13.         sort(strb.begin(),strb.end());
  14.         return stra==strb;
  15. }
  16. int main()
  17. {
  18.         //std::cout<<xiangtong(142875,3*142875);
  19.          for (int n =10;n<=1000000;n++)
  20.          {
  21.                  if(xiangtong(2*n,n) &&xiangtong(3*n,n)&&xiangtong(4*n,n)&&xiangtong(5*n,n)&&xiangtong(6*n,n))
  22.                  {
  23.                          cout<<n<<endl;
  24.                  }
  25.          }
  26.         return 0;
  27. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-12 19:11:20 | 显示全部楼层
142857

  1. for i in range(100000,200000):
  2.         x1 = sorted(list(str(i)))
  3.         x2 = sorted(list(str(i*2)))
  4.         x3 = sorted(list(str(i*3)))
  5.         x4 = sorted(list(str(i*4)))
  6.         x5 = sorted(list(str(i*5)))
  7.         x6 = sorted(list(str(i*6)))
  8.         if x1 == x2 == x3 == x4 == x5 == x6:
  9.                 print (i)
  10.                 exit()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-21 19:50:08 | 显示全部楼层
  1. i = 100
  2. while True:
  3.     if len(str(i))==len(str(2*i))==len(str(3*i))==len(str(5*i))==len(str(6*i)):
  4.         if set(str(i))==set(str(2*i))==set(str(3*i))==set(str(5*i))==set(str(6*i)):
  5.             print(i)
  6.             break
  7.     i+=1
复制代码

结果:142857
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-17 12:46:35 | 显示全部楼层
  1. # encoding:utf-8
  2. # 1x 2x 3x 4x 5x 6x数字相同
  3. from time import time
  4. def euler052(N=1000000):
  5.     for i in range(100000, 170000):
  6.         if set(str(i)) == set(str(2 * i)) == set(str(3 * i)) == set(str(4 * i)) == set(str(5 * i)) == set(str(6 * i)):
  7.             print(i)
  8.             return
  9. if __name__ == '__main__':
  10.     start = time()
  11.     euler052()
  12.     print('cost %.6f sec' % (time() - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-14 16:00:49 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:30 编辑
  1. #!/usr/bin/env python
  2. #coding:utf-8
  3. def Ss():
  4.     n=1
  5.     while True:
  6.         n+=1
  7.         B=0
  8.         for i in list(range(3,7)):
  9.             if sorted(list(str(n*i))) ==sorted(list(str(n*2))):
  10.                 B+=1
  11.                 if B==4:
  12.                     return n


  13. if __name__ == '__main__':
  14.     num=Ss()
  15.     print(num)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-15 10:14:21 | 显示全部楼层
此代码使用matlab编程
Problem52所用时间为: 18.6882秒
Problem52的答案为: 142857
  1. %% Problem52.m
  2. % 最后编辑时间:17-06-14 22:34
  3. % 若正整数x,满足2x,3x,4x,5x,6x均为排列,找到满足此条件的最小数
  4. % Problem52所用时间为: 42.5714秒
  5. % Problem52的答案为: 142857

  6. function Output = Problem52()
  7. tic
  8. Start = 100000;
  9. Judge = 0;
  10. while (Judge == 0)
  11.      Start = Start + 1;
  12.      Judge = PermMul(Start);
  13. end

  14. Output = Start;

  15. toc
  16. disp('此代码使用matlab编程')
  17. disp(['Problem52所用时间为: ',num2str(toc),'秒'])
  18. disp(['Problem52的答案为: ',num2str(Output)])
  19. end
  20. %输入一个数x,检验其2x,3x,4x,5x,6x,是否为排列
  21. %是Judge = 1.不是Judge = 0;
  22. function Judge = PermMul(x)
  23. if nargin == 0
  24. x = 4543;
  25. end
  26. if length(num2str(x)) ~= length(num2str(x*6))
  27.     Judge = 0;
  28. else
  29.     Stand = num2str(x);
  30.     Judge = 1;
  31.     for ii = 2:6
  32.         if strcmp(sort(num2str(x*ii)),sort(Stand)) == 0
  33.             Judge = 0;
  34.             break
  35.         end
  36.     end
  37. end
  38. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-30 12:21:04 | 显示全部楼层
用的matlab
结果是
      142857

时间已过 2.864496 秒。
>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-1 13:18:21 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:30 编辑
  1. import time


  2. def time_it():
  3.         start=time.time()
  4.         fun()
  5.         end=time.time()
  6.         print('cost %.6f Secs'%(end-start))


  7. def fun():
  8.         x=1
  9.         while True:
  10.                 x1=set(str(x))
  11.                 x2=set(str(x * 2))
  12.                 x3=set(str(x * 3))
  13.                 x4=set(str(x * 4))
  14.                 x5=set(str(x * 5))
  15.                 x6=set(str(x * 6))
  16.                 if x1 == x2 == x3 == x4 == x5 == x6:
  17.                         print('%d是最小的正整数 x,使得 2x, 3x, 4x, 5x, 和 6x 都包含同样的数字'%x)
  18.                         print('%d * 2 = %d'%(x,x*2))
  19.                         print('%d * 3 = %d'%(x,x*3))
  20.                         print('%d * 4 = %d'%(x,x*4))
  21.                         print('%d * 5 = %d'%(x,x*5))
  22.                         print('%d * 6 = %d'%(x,x*6))
  23.                         break
  24.                 else:
  25.                         x+=1


  26. 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
'''
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-1 10:02:31 | 显示全部楼层
142857


  1. def numList(num):
  2.     NumStr = str(num)
  3.     temp = []
  4.     for i in range(len((NumStr))):
  5.         temp.append(int(str(num)[i]))
  6.     NumList = list(set(temp))
  7.     return NumList

  8. for i in range(1000000):
  9.     a = i
  10.     b = i*2
  11.     c = i*3
  12.     d = i*4
  13.     e = i*5
  14.     f = i*6
  15.     if len(str(a)) == len(str(b)) == len(str(c))  == len(str(d))  ==len(str(e)) ==len(str(f)):
  16.         if numList(a) == numList(b) == numList(c) == numList(d) == numList(e) == numList(f):
  17.             print(i)
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 16:27:15 | 显示全部楼层
结果是:142857
用时:0.624004 秒
  1. import time


  2. def cal_result():
  3.     number = 100
  4.     while True:
  5.         mark = 1
  6.         for i in range(1, 7):
  7.             if set(str(number)) != set(str(number*i)):
  8.                 mark = 0
  9.                 break
  10.         if mark:
  11.             return number
  12.         number += 1
  13. print("结果是:{}\n用时:{} 秒".format(cal_result(), time.process_time()))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-19 16:55:19 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-4-21 14:08 编辑

还是 C++ 牛批
用时 88ms
  1. #include<iostream>
  2. using namespace std;


  3. bool judge(int i) {
  4.     int j, idx = 6, arr[6][10] = { 0 };

  5.     while (idx) {
  6.         j = i * idx;
  7.         idx--;

  8.         while (j) {
  9.             arr[idx][j % 10u]++;
  10.             j /= 10;
  11.         }
  12.     }

  13.     while (idx < 10u) {
  14.         if (arr[0][idx] != arr[1][idx]\
  15.                 || arr[1][idx] != arr[2][idx]\
  16.                 || arr[2][idx] != arr[3][idx]\
  17.                 || arr[3][idx] != arr[4][idx]\
  18.                 || arr[4][idx] != arr[5][idx]) {
  19.             return true;
  20.         }

  21.         idx++;
  22.     }

  23.     return false;
  24. }


  25. int main() {
  26.     int i;
  27.     for (i = 1; judge(i); i++);
  28.     cout << i << endl;
  29.     return 0;
  30. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-16 17:05:36 | 显示全部楼层
142857

Process returned 0 (0x0)   execution time : 0.043 s
Press any key to continue.
对5个数依次两两判断即可
  1. #include<iostream>
  2. using namespace std;

  3. bool same_digit(int x,int y){
  4.   int digit[10] = {0};

  5.   while(x){
  6.     digit[x % 10]++;
  7.     x /= 10;
  8.   }

  9.   while(y){
  10.     digit[y % 10]--;
  11.     y /= 10;
  12.   }

  13.   for (int i = 0;i < 10;i++)
  14.     if (digit[i]) return false;

  15.   return true;
  16. }

  17. bool judge(int x){
  18.   int t = x * 2;

  19.   for (int i = 3;i <= 6;i++){
  20.     int u = x * i;
  21.     if (!same_digit(t,u)) return false;
  22.   }
  23.   return true;
  24. }

  25. int main(){
  26.   for (int i = 1;;i++){
  27.     if (judge(i)) {cout << i << endl; break;}
  28.   }
  29.   return 0;
  30. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-27 22:48:26 | 显示全部楼层
  1. #include <stdio.h>

  2. main()
  3. {
  4.         int i = 1, j, k, m, flag = 1;
  5.         while (1)
  6.         {
  7.                 i++;
  8.                 j = i;

  9.                 int a[10] = { 0 };//标记法
  10.                 while (j)//将i各个位数用数组a标记
  11.                 {
  12.                         k = j % 10;
  13.                         a[k] = 1;
  14.                         j /= 10;
  15.                 }
  16.                 for (m = 2; m < 7; m++)
  17.                 {
  18.                         j = i * m;
  19.                         while (j)
  20.                         {
  21.                                 k = j % 10;
  22.                                 if (a[k] != 1)//若数组中没有被标记,直接下一轮循环
  23.                                 {
  24.                                         flag = 0;
  25.                                         goto Label;
  26.                                 }
  27.                                 j /= 10;
  28.                         }
  29.                 }
  30.                 if (flag)
  31.                 {
  32.                         printf("%d", i);
  33.                         break;
  34.                 }
  35.         Label:flag = 1;
  36.         }
  37. }
复制代码


142857
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-24 20:16:21 | 显示全部楼层
#找出最小的正整数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秒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-9 08:47:39 | 显示全部楼层
本帖最后由 guosl 于 2022-1-9 08:56 编辑
  1. /*
  2. 欧拉问题52
  3. 答案:142857
  4. 耗时:0.0143542秒(单核)
  5.       0.0026904秒(六核)
  6. */
  7. #include <iostream>
  8. #include <cstring>
  9. #include <cstdlib>
  10. #include <algorithm>
  11. #include <omp.h>
  12. using namespace std;

  13. const int nSteps = 100;

  14. int main(void)
  15. {
  16.   int k = 10;
  17.   int nMin = 0x7fffffff;
  18.   volatile int nContinue = 0;
  19. #pragma omp parallel shared(nContinue) reduction(min:nMin)
  20.   while (nContinue == 0)
  21.   {
  22.     int k1 = 0;
  23. #pragma omp critical
  24.     {
  25.       k1 = k;
  26.       k += nSteps;
  27.     }
  28.     for (int j = k1; nContinue == 0 && j < k1 + nSteps; ++j)
  29.     {
  30.       char str[12], str1[12];
  31.       _itoa_s(j, str, 10);
  32.       sort(str, str + strlen(str));
  33.       bool bFind = true;
  34.       for (int i = 2; i < 7; ++i)
  35.       {
  36.         int x = i * j;
  37.         _itoa_s(x, str1, 10);
  38.         sort(str1, str1 + strlen(str1));
  39.         if (strcmp(str, str1) != 0)
  40.         {
  41.           bFind = false;
  42.           break;
  43.         }
  44.       }
  45.       if (bFind)
  46.       {
  47.         if (j < nMin)
  48.         {
  49.           nMin = j;
  50. #pragma omp atomic
  51.           ++nContinue;
  52.         }
  53.       }
  54.     }
  55.   }
  56.   cout << nMin << endl;
  57.   return 0;
  58. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 23:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表