鱼C论坛

 找回密码
 立即注册
查看: 2893|回复: 15

题目62:找出最小的立方数,使得它各位的排列中五个是立方数

[复制链接]
发表于 2015-8-7 21:51:05 | 显示全部楼层 |阅读模式

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

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

x

Cubic permutations

The cube, 41063625 QQ20150807-4@2x.png can be permuted to produce two other cubes: 56623104 QQ20150807-2@2x.png and 66430125 QQ20150807-3@2x.png . In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.

题目:

立方数 41063625 QQ20150807-4@2x.png 通过排列可以得到两个另外的立方数: 56623104 QQ20150807-2@2x.png 和 66430125 QQ20150807-3@2x.png 。 实际上 41063625 是最小的三个(不多不少)排列是立方数的立方数。

找出最小的立方数,其五个(不多不少)排列是立方数。

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

使用道具 举报

发表于 2016-8-31 13:41:53 | 显示全部楼层
怎么只跑了个3个的 。。除了题目给的。。 。。
1003003001
1030301000
1331000000
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-9 23:25:49 | 显示全部楼层
  1. 5027 127035954683
  2. 7061 352045367981
  3. 7202 373559126408
  4. 8288 569310543872
  5. 8384 589323567104
  6. 0.298000097275

  7. import time
  8. start_1=time.time()
  9. def changeNum(n):
  10.     l=[x for x in str(n)]
  11.     l.sort()
  12.     return ''.join(l)
  13. D,l,num1={},[],0
  14. for i in range(1,10000):
  15.     if changeNum(i**3) in D:
  16.         D[changeNum(i**3)]+=1
  17.     else:
  18.         D[changeNum(i**3)]=0
  19. for d in D:
  20.     if D.get(d)==4:
  21.         l.append(d)
  22. num1=min(l)
  23. for j in range(1,10000):
  24.     if changeNum(j**3)==num1:
  25.         print j,j**3
  26. print time.time()-start_1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-24 10:19:12 | 显示全部楼层
  1. list1=[]
  2. for i in range(10,10000):
  3.     list1.append(i**3)
  4. for i in range(len(list1)):
  5.     temp = i+1
  6.     count=1
  7.     tmp = list(str(list1[i]))
  8.     tmp.sort()
  9.     while temp+1<len(list1):
  10.         cmp=list(str(list1[temp]))
  11.         cmp.sort()
  12.         if tmp == cmp:
  13.             count += 1
  14.         temp += 1
  15.     if count == 5:
  16.         print(list1[i])
  17.         break
复制代码

结果:127035954683。差不多一分钟出结果,慢的不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-19 23:02:06 | 显示全部楼层
  1. # encoding:utf-8
  2. # 找出最小的排列是五个立方数的数
  3. from time import time
  4. def euler062(N=1000):
  5.     l_nums = []
  6.     l_result = []
  7.     n = 345
  8.     while True:
  9.         l_tmp = sorted([x for x in str(n ** 3)])
  10.         l_result.append(l_tmp)
  11.         l_nums.append(n)
  12.         if l_result.count(l_tmp) == 5:
  13.             for k in range(1, 6):
  14.                  print(l_nums[l_result.index(l_tmp)])
  15.                  l_nums.pop(l_result.index(l_tmp))
  16.                  l_result.pop(l_result.index(l_tmp))  
  17.             break
  18.         n += 1
  19. if __name__ == '__main__':
  20.     start = time()
  21.     euler062()
  22.     print('cost %.6f sec' % (time() - start))
复制代码

5027
7061
7202
8288
8384
cost 0.939648 sec
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-6 21:55:25 | 显示全部楼层
  1. from time import time
  2. ttt = time()
  3. def program62_1():
  4.     n, s = 345, {}
  5.     while True:
  6.         temp = tuple(sorted(str(n**3)))
  7.         if temp not in list(s.keys()):
  8.             s[temp] = [n]
  9.         elif len(s[temp]) == 4:
  10.             print(s[temp]+[n])
  11.             break
  12.         else:
  13.             s[temp] += [n]
  14.         n += 1
  15. def program62_2():
  16.     n, s = 345, []
  17.     while True:
  18.         temp = sorted(str(n**3))
  19.         s += [temp]
  20.         if s.count(temp) == 5:
  21.             print([i+345 for i in range(len(s)) if s[i] == temp])
  22.             break
  23.         n += 1
  24. # program62_1()
  25. program62_2()
  26. print(time()-ttt)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-2 20:05:36 | 显示全部楼层
用的matlab
结果是:
127035954683
352045367981
373559126408
569310543872
589323567104
时间已过 4.986045 秒。
>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-15 17:11:47 | 显示全部楼层
aardio编写:
  1. import win.ui;
  2. /*DSG{{*/
  3. mainForm = win.form(text="aardio form";right=686;bottom=314;border="dialog frame";max=false)
  4. mainForm.add(
  5. button={cls="button";text="Calculate";left=91;top=207;right=599;bottom=268;z=2};
  6. edit={cls="edit";left=88;top=67;right=602;bottom=193;edge=1;multiline=1;z=1};
  7. static={cls="static";text="Euler 62";left=93;top=36;right=236;bottom=61;font=LOGFONT(h=-20);transparent=1;z=3}
  8. )
  9. /*}}*/

  10. mainForm.button.oncommand = function(id,event){
  11.         mainForm.button.text = "On Calculating..."
  12.         stime = time.tick();
  13.         mainForm.edit.print(win.invoke(
  14.                 function(mainForm){
  15.                         var tab = {};
  16.                         for i=1;10000 {
  17.                                 i3 = tostring(i**3);
  18.                                 var t = {};
  19.                                 for j=1;string.len(i3) {
  20.                                         table.push(t,string.sub(i3,j,j));
  21.                                 }
  22.                                 table.sort(t);
  23.                                 var n = string.join(t,"");
  24.                                 var flag = true;
  25.                                 for k,v in tab {
  26.                                         if k == n {
  27.                                                 table.push(v,i);
  28.                                                 flag = false;
  29.                                                 break;
  30.                                         }
  31.                                 }
  32.                                 if flag {
  33.                                         tab[n] = {i};
  34.                                 } else {
  35.                                         if table.len(tab[n])==5 {
  36.                                                 return tab[n];
  37.                                         }
  38.                                 }
  39.                         }
  40.                 },mainForm
  41.         ))
  42.         mainForm.edit.print(time.tick()-stime,"ms");
  43.         mainForm.button.text = "Calculate"
  44. }

  45. mainForm.enableDpiScaling();
  46. mainForm.show();

  47. return win.loopMessage();
复制代码


[
    5027,
    7061,
    7202,
    8288,
    8384
]
1906        ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-3 10:47:43 | 显示全部楼层
import time


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


def sort_num(num):
        list_num=[x for x in str(num)]
        list_num.sort()
        return ''.join(list_num)


def fun():
        num=1
        list_num=[['1']]#[['1234',11,22,33]]6591002036752   6591002036833
        content=0
        index=0
        while True:
                if content:
                        break
                sorted_num=sort_num(num ** 3)
                for each in list_num:
                        if sorted_num in each[0]:
                                each.append(num)
                                if len(each) == 6:#五个排列是立方数
                                        content=1
                                        index=list_num.index(each)
                                break
                else:
                        list_num.append([sorted_num,num])
                num+=1
        print_list=list_num[index]
        print_list=print_list[1:]
        for each in print_list:
                print(each,each**3,sort_num(each**3))
       
               
if __name__=='__main__':
        time_it()
'''
5027 127035954683 012334556789
7061 352045367981 012334556789
7202 373559126408 012334556789
8288 569310543872 012334556789
8384 589323567104 012334556789
cost 1.165871 Secs
'''
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-9 16:51:48 | 显示全部楼层
najin 发表于 2017-10-2 20:05
用的matlab
结果是:
127035954683

想知道用MATLAB的代码,如果可以的话可以透露一下吗?非常感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-9 16:52:44 | 显示全部楼层
想知道有没有用MATLAB编码的啊?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-28 11:08:03 | 显示全部楼层
本帖最后由 王小召 于 2019-6-28 11:19 编辑

自己代码太垃圾, 转不过来了!只知道题目要求只有五个,所以都要遍历一遍吧。。但是看楼上代码,貌似碰到5直接中断答案也是一样的不变;

结果是:[(5027, 127035954683), (7061, 352045367981), (7202, 373559126408), (8288, 569310543872), (8384, 589323567104)]
用时:22.0429413 秒

  1. import time


  2. def Func1():
  3.     bool_range = [0] * 100000
  4.     for i, j in enumerate(bool_range):
  5.         if not j:
  6.             start_range = i ** 3
  7.             length = len(str(start_range))
  8.             start = i + 1
  9.             result = [(i, i ** 3)]
  10.             while len(str(start ** 3)) == length:
  11.                 if sorted(list(str(start_range))) == sorted(list(str(start ** 3))):
  12.                     result.append((start, start**3))
  13.                     bool_range[start] = 1
  14.                 start += 1
  15.             if len(result) == 5:
  16.                 return result


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

使用道具 举报

发表于 2020-12-31 16:38:54 From FishC Mobile | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2021-2-17 22:05 编辑

C++ 3ms
  1. #include<iostream>
  2. #include<limits>
  3. #include<unordered_map>



  4. size_t hash_permutation(unsigned long long n) {
  5.     constexpr unsigned long long add[] = { 1ull, 1ull << 5, 1ull << 10, 1ull << 15, 1ull << 20, 1ull << 25, 1ull << 30, 1ull << 35, 1ull << 40, 1ull << 45 };
  6.     constexpr unsigned short ENLARGE = (unsigned long long)(-1) / (0b10100ull << (9 * 5));

  7.     unsigned long long t, result = 0;


  8.     while (n) {
  9.         t = n;
  10.         n /= 10;
  11.         result += add[t - n * 10];
  12.     }


  13.     return result * ENLARGE;
  14. }



  15. struct MapVal {
  16.     unsigned long long min;
  17.     unsigned char count = 0;
  18. };



  19. int main() {
  20.     using namespace std;
  21.     ios::sync_with_stdio(false);

  22.     unordered_map<size_t, MapVal> counter;
  23.     unsigned long long n, cube_of_n;
  24.     MapVal* ptr;


  25.     for (n = 1; n < 10000; n++) {
  26.         cube_of_n = n * n * n;
  27.         ptr = &counter[hash_permutation(cube_of_n)];

  28.         if (!(ptr->count)) {
  29.             ptr->min = cube_of_n;
  30.         }

  31.         ptr->count++;
  32.     }


  33.     n = numeric_limits<unsigned long long>::max();

  34.     for (auto& i : counter) {
  35.         if (i.second.count == 5) {
  36.             if (i.second.min < n) {
  37.                 n = i.second.min;
  38.             }
  39.         }
  40.     }


  41.     cout << n << endl;
  42.     return 0;
  43. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-19 19:50:41 | 显示全部楼层

能不能麻烦大神解释一下 我看不明白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-10 18:14:49 | 显示全部楼层
  1. /*
  2. 答案:127035954683
  3. 耗时:0.776675秒(单核)
  4.       0.10129秒(六核)
  5. */
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <cstdio>
  10. #include <omp.h>
  11. using namespace std;

  12. const int nSteps = 10;

  13. int main(void)
  14. {
  15.   double t = omp_get_wtime();
  16.   int k = 406;
  17.   int nR = 0x7fffffff;
  18.   bool bContinue = true;
  19. #pragma omp parallel shared(k,bContinue) reduction(min:nR)
  20.   while (bContinue)
  21.   {
  22.     int k2 = 0;
  23. #pragma omp critical
  24.     {
  25.       k2 = k;
  26.       k += nSteps;
  27.     }
  28.     for (int k1 = k2; bContinue && k1 < k2 + nSteps; ++k1)
  29.     {
  30.       long long lk = k1;
  31.       lk = lk * lk * lk;
  32.       char str[20];
  33.       sprintf_s(str, "%lld", lk);
  34.       int nL = strnlen_s(str, 20);
  35.       sort(str, str + nL);
  36.       int nCount = 1;
  37.       int j = k1 + 1;
  38.       while (bContinue)
  39.       {
  40.         long long lj = j;
  41.         lj = lj * lj * lj;
  42.         char strj[20];
  43.         sprintf_s(strj, "%lld", lj);
  44.         int nLj = strnlen_s(strj, 20);
  45.         if (nLj > nL)
  46.           break;
  47.         sort(strj, strj + nLj);
  48.         if (strcmp(str, strj) == 0)
  49.           ++nCount;
  50.         ++j;
  51.       }
  52.       if (nCount == 5)
  53.       {
  54.         nR = min(k1, nR);
  55.         bContinue = false;
  56. #pragma omp flush(bContinue)
  57.         break;
  58.       }
  59.     }
  60.   }
  61.   t = omp_get_wtime() - t;
  62.   cout << (long long)nR * nR * nR << endl << t << endl;
  63.   return 0;
  64. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-29 17:54:36 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()


  3. class P62:

  4.     def __init__(self, test_range):
  5.         self.test_range = test_range
  6.         self.cubic_pool = [i ** 3 for i in range(200, test_range)]
  7.         self.bool_num = [True] * (test_range - 345)
  8.         self.count = 1
  9.         self.num_list = []
  10.         self.find_nums()

  11.     @staticmethod
  12.     def is_permutable(num_1, num_2):
  13.         str_num_1, str_num_2 = str(num_1), str(num_2)
  14.         if len(str_num_1) != len(str_num_2):
  15.             return False
  16.         for each_digit in str_num_1:
  17.             if each_digit not in str_num_2:
  18.                 return False
  19.             else:
  20.                 str_num_2 = str_num_2.replace(each_digit, '', 1)
  21.         return True

  22.     def check_num(self, num_1):
  23.         test_num = num_1 + 1
  24.         while test_num < self.test_range:
  25.             if self.bool_num[test_num - 345] and \
  26.                     self.is_permutable(self.cubic_pool[num_1 - 200], self.cubic_pool[test_num - 200]):
  27.                 self.count += 1
  28.                 self.num_list.append(test_num)
  29.                 self.bool_num[test_num - 345] = False
  30.             test_num += 1

  31.     def find_nums(self):
  32.         for num0 in range(345, self.test_range):
  33.             if self.bool_num[num0 - 345]:
  34.                 self.num_list = [num0]
  35.                 self.count = 1
  36.                 self.check_num(num0)
  37.                 if self.count == 5:
  38.                     print(self.num_list)
  39.                     print([self.cubic_pool[j - 200] for j in self.num_list])
  40.                     break


  41. res = P62(10000)
  42. print("It costs %f s" % (t.perf_counter() - start))
复制代码


[5027, 7061, 7202, 8288, 8384]
[127035954683, 352045367981, 373559126408, 569310543872, 589323567104]
It costs 23.116533 s
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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