鱼C论坛

 找回密码
 立即注册
查看: 2842|回复: 12

题目38:最大的能够通过一个固定数与1,2,3,...相乘得到的1到9pandigital数是多少?

[复制链接]
发表于 2015-5-2 11:21:22 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 欧拉计划 于 2015-5-2 11:22 编辑
Pandigital multiples

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?

题目:

将 192 与 1, 2, 3 分别相乘得到:

192 × 1 = 192
192 × 2 = 384
192 × 3 = 576

将这三个乘积连接起来我们得到一个 1 到 9 的 pandigital 数, 192384576。我们称 192384576 是 192 和 (1,2,3) 的连接积。

通过将 9 与 1, 2, 3, 4 和 5 相乘也可以得到 pandigital 数:918273645,这个数是 9 和 (1,2,3,4,5) 的连接积。

用一个整数与 1,2, ... , n(n 大于 1)的连接积构造而成的 1 到 9 pandigital 数中最大的是多少?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-30 12:16:23 | 显示全部楼层
没懂什么意思,,不过感觉不难。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-6 11:49:14 | 显示全部楼层
  1. list1 = [1,2,3,4,5,6,7,8,9]
  2. list2 = []
  3. for i in range(1,100000):
  4.       str_1 = ''
  5.       for each in list1:
  6.             str_1 += str(i*each)
  7.             if len(str_1)>=9:
  8.                   break
  9.       
  10.       if len(str_1) == 9:
  11.             tmp = True
  12.             for each in list1:
  13.                   if str(each) not in str_1:
  14.                         tmp = False
  15.                         break
  16.             if tmp:
  17.                   list2.append(int(str_1))
  18.                   list2.sort()
  19. print(list2[-1])
  20.                   
复制代码

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

使用道具 举报

发表于 2017-1-14 14:20:04 | 显示全部楼层
  1. # encoding:utf-8
  2. # 一个整数与1,2,3...N的乘积链接构成1-9的pandigital,寻找最大的数
  3. from time import time
  4. def euler038():
  5.     for i in range(1, 9877):
  6.         tmp = ''
  7.         for t in range(1, 10):
  8.             tmp += str(i * t)
  9.             if len(tmp) > 9:
  10.                 break
  11.             if len(tmp) == 9:
  12.                 if not '123456789'.strip(tmp):
  13.                     print(i, [k for k in range(1, t + 1)])
  14.                     break
  15. if __name__ == '__main__':
  16.     start = time()
  17.     euler038()
  18.     print('cost %.6f sec' % (time() - start))
复制代码


1 [1, 2, 3, 4, 5, 6, 7, 8, 9]
9 [1, 2, 3, 4, 5]
192 [1, 2, 3]
219 [1, 2, 3]
273 [1, 2, 3]
327 [1, 2, 3]
6729 [1, 2]
6792 [1, 2]
6927 [1, 2]
7269 [1, 2]
7293 [1, 2]
7329 [1, 2]
7692 [1, 2]
7923 [1, 2]
7932 [1, 2]
9267 [1, 2]
9273 [1, 2]
9327 [1, 2]
cost 0.020014 sec
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-15 00:18:36 | 显示全部楼层
%% Problem38.m
% 最后编辑时间:17-06-15 0:45
% 通过将 9 与 1, 2, 3, 4 和 5 相乘也可以得到 pandigital 数:918273645,这个数是918273645,这个数是 9 和 (1,2,3,4,5) 的连接积。
% 用一个整数与 1,2, ... , n(n 大于 1)的连接积构造而成的 1 到 9 pandigital 数中最大的数是多少?
%分析 :1位数1 *[1 2 3 4 5 6 7 8 9] 9*[ 1 2 3 4 5]
%      2位数 * 不可能存在这样的数
%      3位数 * [1 2 3] 三位数小于 333
%      4位数 * [1 2]   四位数大于5000
  1. %% Problem38.m
  2. % 最后编辑时间:17-06-15 0:45
  3. % 通过将 9 与 1, 2, 3, 4 和 5 相乘也可以得到 pandigital 数:918273645,这个数是918273645,这个数是 9 和 (1,2,3,4,5) 的连接积。
  4. % 用一个整数与 1,2, ... , n(n 大于 1)的连接积构造而成的 1 到 9 pandigital 数中最大的数是多少?
  5. %分析 :1位数1 *[1 2 3 4 5 6 7 8 9] 9*[ 1 2 3 4 5]
  6. %      2位数 * 不可能存在这样的数
  7. %      3位数 * [1 2 3] 三位数小于 333
  8. %      4位数 * [1 2]   四位数大于5000
  9. % Problem38所用时间为: 1.43秒
  10. % Problem358的答案为: 932718654
  11. function Output = Problem38()
  12. tic
  13. Output = 0;
  14. % 一位数
  15. for ii = 1:9
  16.     Str = [];
  17.     FirstJudge = 0;
  18.     for jj = 1:9
  19.         Str = [Str,num2str(ii*jj)];
  20.         if length(Str) == 9
  21.             FirstJudge = 1;
  22.             break
  23.         elseif length(Str) > 9
  24.             FirstJudge = 0;
  25.             break
  26.         end
  27.     end
  28.    
  29.     if FirstJudge == 1
  30.         if str2double(Str) > Output
  31.             if strcmp(sort(Str),'123456789') == 1
  32.                
  33.                 Output = str2double(Str);
  34.             end
  35.         end
  36.     end
  37. end

  38. %三位数
  39. for kk = 100:333
  40.     Str = [num2str(kk),num2str(kk*2),num2str(kk*3)];
  41.     if str2double(Str) > Output
  42.         if strcmp(sort(Str),'123456789') == 1
  43.             
  44.             Output = str2double(Str);
  45.         end
  46.     end
  47. end

  48. %四位数

  49. for ll = 5001:9999
  50.     Str = [num2str(ll),num2str(ll*2)];
  51.     if str2double(Str) > Output
  52.         if strcmp(sort(Str),'123456789') == 1         
  53.             Output = str2double(Str);
  54.         end
  55.     end
  56. end
  57.                   
  58.         
  59. toc
  60. disp('此代码使用matlab编程')
  61. disp(['Problem38所用时间为: ',num2str(toc),'秒'])
  62. disp(['Problem358的答案为: ',num2str(Output)])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-13 12:00:58 | 显示全部楼层
  1. from time import time
  2. start = time()

  3. def isPandingital(n):
  4.     i =1
  5.     str_1 = ''
  6.     tag = True
  7.     while i<10:
  8.         str_1 = str_1+str(n*i)
  9.         i = i + 1
  10.         if '0' not in str_1:
  11.             if  len(set(str_1))==9 and len(str_1)==9:
  12.                    return True
  13.                    break
  14.     return False

  15. def getPandingital(n):
  16.     i = 1
  17.     str_1 = ''
  18.     tag = True
  19.     while i < 10:
  20.         str_1 = str_1+str(n * i)
  21.         i = i + 1
  22.         if '0' not in str_1:
  23.             if len(set(str_1)) == 9 and len(str_1) == 9:
  24.                 return int(str_1)

  25. num = 10000    #n需要大于1,所以需要两个数组合,num不会超过4位数,为保险,可以取num = 100000
  26. while num > 0:
  27.    if  not isPandingital(num):
  28.        num -=1
  29.    else:
  30.        print(num)
  31.        print(getPandingital(num))
  32.        break

  33. print("Program running cost %4f secs!" % (time() - start))
复制代码


9327
932718654
Program running cost 0.005000 secs!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-30 11:00:24 | 显示全部楼层
用的matlab
结果是:
   932718654

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

使用道具 举报

发表于 2019-3-28 16:17:07 | 显示全部楼层

  1. def concatenating(num):
  2.     sum = 0
  3.     numTempList = []
  4.     for i in range(1, 10):
  5.         sum += len(str(num * i))
  6.         numTempList.append(str(num * i))
  7.         if sum == 9:
  8.             break
  9.         elif sum > 9:
  10.             break
  11.     finalNumStr = ""
  12.     for each in numTempList:
  13.         finalNumStr += each
  14.     finalNum = int(finalNumStr)
  15.     return finalNum

  16. def isPandigital(num):
  17.     if len(str(num)) != 9:
  18.         return False
  19.     else:
  20.         numTempList = []
  21.         refNumList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  22.         for i in range(len(str(num))):
  23.             numTempList.append(int(str(num)[i]))
  24.         if list(set(numTempList)) == refNumList:
  25.             return True
  26.         else:
  27.             return False

  28. numList = []
  29. for num in range(1, 1000000):
  30.     if isPandigital(concatenating(num)):
  31.         numList.append(concatenating(num))

  32. numList.sort()
  33. print(numList[-1])
复制代码

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

使用道具 举报

发表于 2019-6-14 10:11:00 | 显示全部楼层
本帖最后由 王小召 于 2019-6-14 10:24 编辑

最大值是:932718654
用时:0.4212027 秒
  1. import time

  2. def is_pandigital(num):
  3.     l = [i for i in str(num)]
  4.     l.sort()
  5.     if l == [str(i) for i in range(1, 10)]:
  6.         return True
  7.     else:
  8.         return False

  9. def cal_nums():
  10.     results = []
  11.     for num in range(1, 100000):
  12.         length = 2
  13.         tmp = ""
  14.         for i in range(1, 10):
  15.             tmp += str(num * i)
  16.             if len(str(tmp)) < 9:
  17.                 continue
  18.             elif len(str(tmp)) > 9:
  19.                 break
  20.             else:
  21.                 if is_pandigital(tmp):
  22.                     results.append((tmp, num, length))

  23.     return results

  24. results = cal_nums()
  25. max_result = max(int(each[0]) for each in results)

  26. print("最大值是:{}\n用时:{} 秒".format(max_result, time.process_time()))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-16 19:10:57 | 显示全部楼层
932718654

Process returned 0 (0x0)   execution time : 0.036 s
Press any key to continue.
既然题目已给出918273645这一解,可直接讨论首位为9的情况
一位数的构造只有上述一种
其余情况只有四位数满足题意
由连接积的定义,逆序枚举效率更高
  1. #include<iostream>
  2. using namespace std;

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

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

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

  13.   if (digit[0]) return false;

  14.   for (int i = 1;i < 10;i++)
  15.     if (!digit[i]) return false;

  16.   return true;
  17. }

  18. int main(){
  19.   for (int i = 9876;i > 9011;i--){
  20.     int t = 2*i;
  21.     if (pan(i,2*i)) {cout << i << t << endl; break;}
  22.   }
  23.   return 0;
  24. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-17 09:42:17 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. int check_num(int);
  4. int check_num(int num)
  5. {
  6.         int i, j, a[10] = { 0 };
  7.         j = num;
  8.         while (j)
  9.         {
  10.                 i = j % 10;
  11.                 if (i == 0)
  12.                 {
  13.                         return 0;
  14.                 }
  15.                 if (a[i])
  16.                 {
  17.                         return 0;
  18.                 }
  19.                 j /= 10;
  20.                 a[i] = 1;
  21.         }
  22.         return 1;
  23. }

  24. main()
  25. {
  26.         char str[20], rts[20];

  27.         int i, j, k, max = 0;

  28.         for (i = 5000; i < 10000; i++)
  29.         {
  30.                 j = i * 2;
  31.                 sprintf(str, "%d", i);
  32.                 sprintf(rts, "%d", j);
  33.                 strcat(str, rts);
  34.                 k = atoi(str);

  35.                 if (check_num(k))
  36.                 {
  37.                         max = max > i ? max : i;                       
  38.                 }
  39.         }

  40.         printf("%d", max);
  41. }
复制代码



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

使用道具 举报

发表于 2022-10-24 17:02:08 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()


  3. def is_pandigital(a_num):
  4.     tar = [index for index in range(1, 10)]
  5.     list_num = [int(each) for each in str(a_num)]
  6.     if len(list_num) != 9:
  7.         return False
  8.     elif tar == list(set(list_num)):
  9.         return True


  10. pandigitals = []
  11. for i in range(2, 10):
  12.     multiplier = 10 ** (int(9 // i) - 1)
  13.     while True:
  14.         res = ''
  15.         for j in range(1, i + 1):
  16.             res += str(multiplier * j)
  17.         res = int(res)
  18.         if res > 1e9:
  19.             break
  20.         elif is_pandigital(res):
  21.             pandigitals.append(res)
  22.         multiplier += 1

  23. print(max(pandigitals))
  24. print("It costs %f s" % (t.perf_counter() - start))
复制代码



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

使用道具 举报

发表于 2023-10-20 11:13:44 | 显示全部楼层
  1. $ time ./main
  2. 932718654

  3. real        0m0.002s
  4. user        0m0.000s
  5. sys        0m0.002s
复制代码
  1. fn bits(x: u32) -> u32 {
  2.     let mut x = x;
  3.     let mut ans = 0;
  4.     while x != 0 {
  5.         if x%10 == 0 {
  6.             return 0;
  7.         }
  8.         let y = 1 << (x % 10 - 1);
  9.         if ans & y != 0 {
  10.             return 0;
  11.         }
  12.         ans |= y;
  13.         x /= 10;
  14.     }
  15.     ans
  16. }

  17. fn is_pandigital(x: u32) -> bool {
  18.     let mut tot = 0;
  19.     for i in 1..=9 {
  20.         let b = bits(i*x);
  21.         if b == 0 || tot & b != 0 {
  22.             return false;
  23.         }
  24.         tot |= b;
  25.         if tot == 0b111111111 {
  26.             return true;
  27.         }
  28.     }
  29.     false
  30. }

  31. fn pandigital(x: u32) -> u32 {
  32.     let v = &mut [0;9];
  33.     let mut vi = 0;
  34.     let mut i = 1;
  35.     while vi != 9 {
  36.         let mut y = x*i;
  37.         while y != 0 {
  38.             v[vi] = y / 10u32.pow(y.ilog10());
  39.             vi += 1;
  40.             y %= 10u32.pow(y.ilog10());
  41.         }
  42.         i += 1;
  43.     }
  44.     let mut sum = 0;
  45.     for i in 0..=8 {
  46.         sum += v[8-i] * 10u32.pow(i as u32);
  47.     }
  48.     sum
  49. }

  50. #[cfg(test)]
  51. mod tests {
  52.     use super::*;
  53.     #[test]
  54.     fn test_is_pandigital() {
  55.         assert!(is_pandigital(192));
  56.         assert!(is_pandigital(9));
  57.         assert!(is_pandigital(1));
  58.         assert!(!is_pandigital(191));
  59.     }
  60.     #[test]
  61.     fn test_pandigital() {
  62.         assert_eq!(pandigital(9),918273645);
  63.         assert_eq!(pandigital(192),192384576);
  64.     }
  65. }

  66. fn main () {
  67.     // 要使 n > 1, 最多是4位数
  68.     let v:Vec<u32> = (1..1e5 as u32).filter(|&x| is_pandigital(x)).collect();
  69.     let p:Vec<u32> = v.into_iter().map(|x| pandigital(x)).collect();
  70.     println!("{}", p.iter().max().unwrap());
  71. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 08:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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