用 mathematica 编程,计算符合要求的质数集合
示例1:{859,401,263,7}示例2:{409,61,83,2,5,7}
上面表示了两个质数集合,每个集合中的任一元素都是质数,且都小于999。每个集合中的所有质数都是由 0,1,2,3,4,5,6,7,8,9 这十个数码构成的,每个数码只用一次,不重不漏。
问: 一共有多少不同的符合上述要求的集合? 每一种集合都要列出来。
我想用 mathematica 编程,思路是把小于 999 的、并且各位数字互不相同的质数分成 22 组:
(1)2。
(2)3。
(3)5。
(4)7。
(5)13,17,19。
(6)23,29。
(7)31,37。
(8)41,43,47。
(9)53,59。
(10)61,67。
(11)71,73,79。
(12)83,89。
(13)97。
(14) 103,107,109,127,137,139,149,157,163,167,173,179,193,197。
(15) 239,241,251,257,263,269,271,281,283,293。
(16) 307,317,347,349,359,367,379,389,397。
(17) 401,409,419,421,431,439,457,461,463,467,479,487,491。
(18) 503,509,521,523,541,547,563,569,571,587,593。
(19) 601,607,613,617,619,631,641,643,647,653,659,673,683,691。
(20) 701,709,719,739,743,751,761,769。
(21) 809,821,823,827,829,839,853,857,859,863。
(22) 907,937,941,947,953,967,971,983。
由题目条件可知,对于任何一个集合,不能有 2 个或更多元素从上面同一组中选取。这只是一个必要条件而已。接下来怎么编程呢?
再举一些符合要求的集合例子哈:
{103,269,487,5}
{103,829,467,5}
{103,829,647,5}
{103,569,487,2}
{103,659,487,2}
{103,859,467,2}
{103,859,647,2}
以 0 为基础考虑,0 不能作为个位或百位,只能是十位。
如果集合中有一个是 103,好像就只有上述这 7 种情况了。
还有 13 种 0 在十位的情况:
107, 109, 307, 401, 409, 503, 509, 601, 607, 701,709, 809,907。
如果把上述 13 种情况的每一种都枚举一遍,就能得到全部的集合。但是手工枚举太繁琐了,怎样编程实现呢?
AbsoluteTiming,0]&]&,(EligiblePrimes=Select[#,Sort[#]==Union[#]&]&])&,Array&,PrimePi];
Do,i]&],Function[{NowEligiblePrimes},Apply==Union[#]&],Sort[#],{}]&,\]],{\,NowEligiblePrimes}],{}],{\,Select,i]]&]}]]],i]&]]],{i,9}];
Sort]] 呵呵,楼上的老师很是厉害呀,运行了您的程序,在我这机器上只用了1秒钟。
{2, 103, 467, 859}, {2, 103, 487, 569}, {2, 103, 487, 659}, {2, 103, 647, 859}, {2, 107, 463, 859}, {2, 107, 643,859}, {2, 109, 457, 683}, {2, 109, 457, 863}, {2, 109, 463, 587}, {2, 109, 463, 857},
{2, 109, 467, 853}, {2, 109, 487, 563}, {2, 109, 487, 653}, {2, 109, 547, 683}, {2, 109, 547, 863}, {2, 109, 587, 643}, {2, 109, 643, 857}, {2, 109, 647, 853}, {2, 149, 607, 853}, {2, 157, 409, 683},
{2, 157, 409, 863}, {2, 157, 463, 809}, {2, 157, 643, 809}, {2, 163, 409, 587}, {2, 163, 409, 857}, {2, 163, 457, 809}, {2, 163, 487, 509}, {2, 163, 547, 809}, {2, 167, 409, 853}, {2, 307, 461, 859},
{2, 307, 641, 859}, {2, 347, 601, 859}, {2, 349, 587, 601}, {2, 349, 601, 857}, {2, 359, 487, 601}, {2, 367, 401, 859}, {2, 367, 541, 809}, {2, 389, 457, 601}, {2, 389, 541, 607}, {2, 389, 547, 601},
{2, 401, 673, 859}, {2, 401, 769, 853}, {2, 401, 853, 967}, {2, 409, 571, 683}, {2, 409, 571, 863}, {2, 409, 587, 613}, {2, 409, 587, 631}, {2, 409, 613, 857}, {2, 409, 617, 853}, {2, 409, 631, 857},
{2, 409, 683, 751}, {2, 409, 751, 863}, {2, 409, 761, 853}, {2, 419, 607, 853}, {2, 431, 607, 859}, {2, 439, 587, 601}, {2, 439, 601, 857}, {2, 457, 601, 839}, {2, 457, 601, 983}, {2, 457, 613, 809},
{2, 457, 631, 809}, {2, 461, 709, 853}, {2, 461, 853, 907}, {2, 463, 571, 809}, {2, 463, 701, 859}, {2, 463, 751, 809}, {2, 479, 601, 853}, {2, 487, 503, 619}, {2, 487, 503, 691}, {2, 487, 509, 613},
{2, 487, 509, 631}, {2, 487, 593, 601}, {2, 487, 601, 953}, {2, 491, 607, 853}, {2, 541, 607, 839}, {2, 541, 607, 983}, {2, 541, 673, 809}, {2, 541, 683, 709}, {2, 541, 683, 907}, {2, 541, 709, 863},
{2, 541, 863, 907}, {2, 547, 601, 839}, {2, 547, 601, 983}, {2, 547, 613, 809}, {2, 547, 631, 809}, {2, 571, 643, 809}, {2, 601, 743, 859}, {2, 601, 853, 947}, {2, 607, 853, 941}, {2, 641, 709, 853},
{2, 641, 853, 907}, {2, 643, 701, 859}, {2, 643, 751, 809}, {3, 241, 607, 859}, {3, 251, 467, 809}, {3, 251, 647, 809}, {3, 257, 461, 809}, {3, 257, 641, 809}, {3, 269, 401, 587}, {3, 269, 401, 857},
{3, 281, 467, 509}, {3, 281, 509, 647}, {3, 401, 569, 827}, {3, 401, 659, 827}, {3, 421, 607, 859}, {3, 457, 601, 829}, {3, 461, 509, 827}, {3, 467, 509, 821}, {3, 467, 521, 809}, {3, 509, 641, 827},
{3, 509, 647, 821}, {3, 521, 647, 809}, {3, 541, 607, 829}, {3, 547, 601, 829}, {5, 103, 269, 487}, {5, 103, 467, 829}, {5, 103, 647, 829}, {5, 107, 463, 829}, {5, 107, 643, 829}, {5, 109, 263, 487},
{5, 109, 283, 467}, {5, 109, 283, 647}, {5, 109, 463, 827}, {5, 109, 467, 823}, {5, 109, 643, 827}, {5, 109, 647, 823}, {5, 127, 409, 683}, {5, 127, 409, 863}, {5, 127, 463, 809}, {5, 127, 643, 809},
{5, 149, 283, 607}, {5, 149, 607, 823}, {5, 163, 409, 827}, {5, 167, 283, 409}, {5, 167, 409, 823}, {5, 239, 487, 601}, {5, 241, 367, 809}, {5, 241, 389, 607}, {5, 241, 607, 839}, {5, 241, 607, 983},
{5, 241, 673, 809}, {5, 241, 683, 709}, {5, 241, 683, 907}, {5, 241, 709, 863}, {5, 241, 863, 907}, {5, 271, 409, 683}, {5, 271, 409, 863}, {5, 271, 463, 809}, {5, 271, 643, 809}, {5, 281, 349, 607},
{5, 281, 367, 409}, {5, 281, 409, 673}, {5, 281, 439, 607}, {5, 281, 463, 709}, {5, 281, 463, 907}, {5, 281, 643, 709}, {5, 281, 643, 907}, {5, 283, 401, 769}, {5, 283, 401, 967}, {5, 283, 409, 617},
{5, 283, 409, 761}, {5, 283, 419, 607}, {5, 283, 461, 709}, {5, 283, 461, 907}, {5, 283, 479, 601}, {5, 283, 491, 607}, {5, 283, 601, 947}, {5, 283, 607, 941}, {5, 283, 641, 709}, {5, 283, 641, 907},
{5, 293, 487, 601}, {5, 307, 461, 829}, {5, 307, 641, 829}, {5, 347, 601, 829}, {5, 349, 601, 827}, {5, 349, 607, 821}, {5, 367, 401, 829}, {5, 367, 409, 821}, {5, 367, 421, 809}, {5, 389, 421, 607},
{5, 401, 673, 829}, {5, 401, 769, 823}, {5, 401, 823, 967}, {5, 409, 613, 827}, {5, 409, 617, 823}, {5, 409, 631, 827}, {5, 409, 673, 821}, {5, 409, 761, 823}, {5, 419, 607, 823}, {5, 421, 607, 839},
{5, 421, 607, 983}, {5, 421, 673, 809}, {5, 421, 683, 709}, {5, 421, 683, 907}, {5, 421, 709, 863}, {5, 421, 863, 907}, {5, 431, 607, 829}, {5, 439, 601, 827}, {5, 439, 607, 821}, {5, 461, 709, 823},
{5, 461, 823, 907}, {5, 463, 701, 829}, {5, 463, 709, 821}, {5, 463, 821, 907}, {5, 479, 601, 823}, {5, 491, 607, 823}, {5, 601, 743, 829}, {5, 601, 823, 947}, {5, 607, 823, 941}, {5, 641, 709, 823},
{5, 641, 823, 907}, {5, 643, 701, 829}, {5, 643, 709, 821}, {5, 643, 821, 907}, {7, 241, 509, 683}, {7, 241, 509, 863}, {7, 241, 563, 809}, {7, 241, 653, 809}, {7, 251, 409, 683}, {7, 251, 409, 863},
{7, 251, 463, 809}, {7, 251, 643, 809}, {7, 263, 401, 859}, {7, 263, 541, 809}, {7, 269, 401, 853}, {7, 281, 409, 563}, {7, 281, 409, 653}, {7, 281, 463, 509}, {7, 281, 509, 643}, {7, 283, 401, 569},
{7, 283, 401, 659}, {7, 283, 461, 509}, {7, 283, 509, 641}, {7, 401, 563, 829}, {7, 401, 569, 823}, {7, 401, 653, 829}, {7, 401, 659, 823}, {7, 409, 521, 683}, {7, 409, 521, 863}, {7, 409, 563, 821},
{7, 409, 653, 821}, {7, 421, 509, 683}, {7, 421, 509, 863}, {7, 421, 563, 809}, {7, 421, 653, 809}, {7, 461, 503, 829}, {7, 461, 509, 823}, {7, 461, 523, 809}, {7, 463, 509, 821}, {7, 463, 521, 809},
{7, 503, 641, 829}, {7, 509, 641, 823}, {7, 509, 643, 821}, {7, 521, 643, 809}, {7, 523, 641, 809}, {23, 41, 607, 859}, {23, 47, 601, 859}, {23, 59, 487, 601}, {23, 61, 409, 587}, {23, 61, 409, 857},
{23, 61, 457, 809}, {23, 61, 487, 509}, {23, 61, 547, 809}, {23, 67, 401, 859}, {23, 67, 541, 809}, {23, 89, 457, 601}, {23, 89, 541, 607}, {23, 89, 547, 601}, {29, 41, 607, 853}, {29, 43, 587, 601},
{29, 43, 601, 857}, {29, 47, 601, 853}, {29, 53, 487, 601}, {29, 61, 487, 503}, {29, 67, 401, 853}, {29, 83, 457, 601}, {29, 83, 541, 607}, {29, 83, 547, 601}, {41, 53, 607, 829}, {41, 59, 283, 607},
{41, 59, 607, 823}, {41, 67, 283, 509}, {41, 67, 503, 829}, {41, 67, 509, 823}, {41, 67, 523, 809}, {41, 89, 523, 607}, {43, 59, 281, 607}, {43, 59, 601, 827}, {43, 59, 607, 821}, {43, 61, 257, 809},
{43, 61, 509, 827}, {43, 67, 251, 809}, {43, 67, 281, 509}, {43, 67, 509, 821}, {43, 67, 521, 809}, {43, 89, 251, 607}, {43, 89, 257, 601}, {43, 89, 521, 607}, {47, 53, 601, 829}, {47, 59, 283, 601},
{47, 59, 601, 823}, {47, 61, 283, 509}, {47, 61, 503, 829}, {47, 61, 509, 823}, {47, 61, 523, 809}, {47, 89, 523, 601}, {53, 61, 409, 827}, {53, 67, 241, 809}, {53, 67, 281, 409}, {53, 67, 401, 829},
{53, 67, 409, 821}, {53, 67, 421, 809}, {53, 89, 241, 607}, {53, 89, 421, 607}, {59, 67, 283, 401}, {59, 67, 401, 823}, {59, 83, 241, 607}, {59, 83, 421, 607}, {61, 83, 257, 409}, {67, 83, 241, 509},
{67, 83, 251, 409}, {67, 83, 409, 521}, {67, 83, 421, 509}, {67, 89, 241, 503}, {67, 89, 401, 523}, {67, 89, 421, 503}, {2, 3, 41, 607, 859}, {2, 3, 47, 601, 859}, {2, 3, 59, 487, 601}, {2, 3, 61, 409, 587},
{2, 3, 61, 409, 857}, {2, 3, 61, 457, 809}, {2, 3, 61, 487, 509}, {2, 3, 61, 547, 809}, {2, 3, 67, 401, 859}, {2, 3, 67, 541, 809}, {2, 3, 89, 457, 601}, {2, 3, 89, 541, 607}, {2, 3, 89, 547, 601}, {2, 5, 13, 467, 809},
{2, 5, 13, 647, 809}, {2, 5, 17, 409, 683}, {2, 5, 17, 409, 863}, {2, 5, 17, 463, 809}, {2, 5, 17, 643, 809}, {2, 5, 31, 467, 809}, {2, 5, 31, 647, 809}, {2, 5, 37, 461, 809}, {2, 5, 37, 641, 809}, {2, 5, 41, 367, 809},
{2, 5, 41, 389, 607}, {2, 5, 41, 607, 839}, {2, 5, 41, 607, 983}, {2, 5, 41, 673, 809}, {2, 5, 41, 683, 709}, {2, 5, 41, 683, 907}, {2, 5, 41, 709, 863}, {2, 5, 41, 863, 907}, {2, 5, 43, 167, 809}, {2, 5, 43, 617, 809},
{2, 5, 43, 761, 809}, {2, 5, 47, 109, 683}, {2, 5, 47, 109, 863}, {2, 5, 47, 163, 809}, {2, 5, 47, 389, 601}, {2, 5, 47, 601, 839}, {2, 5, 47, 601, 983}, {2, 5, 47, 613, 809}, {2, 5, 47, 631, 809}, {2, 5, 61, 347, 809},
{2, 5, 61, 743, 809}, {2, 5, 67, 389, 401}, {2, 5, 67, 401, 839}, {2, 5, 67, 401, 983}, {2, 5, 67, 431, 809}, {2, 5, 71, 409, 683}, {2, 5, 71, 409, 863}, {2, 5, 71, 463, 809}, {2, 5, 71, 643, 809}, {2, 5, 73, 461, 809},
{2, 5, 73, 641, 809}, {2, 5, 79, 401, 683}, {2, 5, 79, 401, 863}, {2, 5, 83, 109, 467}, {2, 5, 83, 109, 647}, {2, 5, 83, 149, 607}, {2, 5, 83, 167, 409}, {2, 5, 83, 401, 769}, {2, 5, 83, 401, 967}, {2, 5, 83, 409, 617},
{2, 5, 83, 409, 761}, {2, 5, 83, 419, 607}, {2, 5, 83, 461, 709}, {2, 5, 83, 461, 907}, {2, 5, 83, 479, 601}, {2, 5, 83, 491, 607}, {2, 5, 83, 601, 947}, {2, 5, 83, 607, 941}, {2, 5, 83, 641, 709}, {2, 5, 83, 641, 907},
{2, 5, 89, 103, 467}, {2, 5, 89, 103, 647}, {2, 5, 89, 107, 463}, {2, 5, 89, 107, 643}, {2, 5, 89, 307, 461}, {2, 5, 89, 307, 641}, {2, 5, 89, 347, 601}, {2, 5, 89, 367, 401}, {2, 5, 89, 401, 673}, {2, 5, 89, 431, 607},
{2, 5, 89, 463, 701}, {2, 5, 89, 601, 743}, {2, 5, 89, 643, 701}, {2, 5, 97, 401, 683}, {2, 5, 97, 401, 863}, {2, 7, 41, 509, 683}, {2, 7, 41, 509, 863}, {2, 7, 41, 563, 809}, {2, 7, 41, 653, 809}, {2, 7, 43, 601, 859},
{2, 7, 53, 461, 809}, {2, 7, 53, 641, 809}, {2, 7, 59, 401, 683}, {2, 7, 59, 401, 863}, {2, 7, 61, 409, 853}, {2, 7, 83, 401, 569}, {2, 7, 83, 401, 659}, {2, 7, 83, 461, 509}, {2, 7, 83, 509, 641}, {2, 7, 89, 401, 563},
{2, 7, 89, 401, 653}, {2, 7, 89, 461, 503}, {2, 7, 89, 503, 641}, {2, 41, 53, 67, 809}, {2, 41, 53, 89, 607}, {2, 41, 59, 83, 607}, {2, 41, 67, 83, 509}, {2, 41, 67, 89, 503}, {2, 47, 53, 61, 809}, {2, 47, 53, 89, 601},
{2, 47, 59, 83, 601}, {2, 47, 61, 83, 509}, {2, 47, 61, 89, 503}, {2, 53, 67, 89, 401}, {2, 59, 67, 83, 401}, {3, 5, 29, 487, 601}, {3, 5, 41, 607, 829}, {3, 5, 47, 601, 829}, {3, 5, 61, 409, 827}, {3, 5, 67, 241, 809},
{3, 5, 67, 281, 409}, {3, 5, 67, 401, 829}, {3, 5, 67, 409, 821}, {3, 5, 67, 421, 809}, {3, 5, 89, 241, 607}, {3, 5, 89, 421, 607}, {5, 7, 23, 461, 809}, {5, 7, 23, 641, 809}, {5, 7, 29, 401, 683}, {5, 7, 29, 401, 863},
{5, 7, 41, 263, 809}, {5, 7, 43, 601, 829}, {5, 7, 61, 283, 409}, {5, 7, 61, 409, 823}, {5, 7, 83, 269, 401}, {5, 7, 89, 263, 401}, {5, 23, 41, 67, 809}, {5, 23, 41, 89, 607}, {5, 23, 47, 61, 809}, {5, 23, 47, 89, 601},
{5, 23, 67, 89, 401}, {5, 29, 41, 83, 607}, {5, 29, 47, 83, 601}, {5, 29, 67, 83, 401}, {2, 3, 5, 7, 461, 809}, {2, 3, 5, 7, 641, 809}, {2, 3, 5, 41,67, 809}, {2, 3, 5, 41, 89, 607}, {2, 3, 5, 47, 61, 809}, {2, 3,5, 47, 89, 601},
{2, 3, 5, 67, 89, 401}, {2, 5, 7, 43, 61, 809}, {2, 5, 7, 43, 89, 601}, {2, 5, 7, 61, 83, 409} 另外问一下,在上述每一个集合中,各元素相加之和最小的是最后那个:2+5+7+61+83+409 = 567,那么相加之和最大的是哪一个呢? SortBy PrimeLists是封装在Module里的局部变量,你放到Module外当然错误。
SortBy是用来替换Sort 用SortBy 代替 Sort运行后得到,最大的和等于 2403,有三组解:{2, 601, 853, 947}, {2, 607, 853, 941}, {2, 641, 853, 907}。 请问 zeroieme 老师,有没有卖 mathematica 编程书的? 一般太小儿科的不想要(就是想要提高班的),希望有较多的实例,语句注解很详细的那种。
如果没有这书,我想老师您自己可以考虑写一本,A4 幅面 500 页左右。一定能够畅销,我先预订一本。 TSC999 发表于 2016-9-12 14:02
请问 zeroieme 老师,有没有卖 mathematica 编程书的? 一般太小儿科的不想要(就是想要提高班的),希望有 ...
我不是老师,只是工厂的技术员。就在大学数模队学过mathematica。
用mathematica是为了简化函数。后来有的函数太大了,不得不编程解决。就靠其他高级语言底子套上控制语句。加上mathematica自带帮助一点点摸出来的。
这个论坛的wayne给了我不小的帮助。
页:
[1]