Forgot password?
 Create new account
View 939|Reply 15

共有28种方法表一千为两素数之和

[Copy link]

462

Threads

969

Posts

9934

Credits

Credits
9934

Show all posts

青青子衿 Posted at 2020-9-23 18:50:14 |Read mode
Last edited by 青青子衿 at 2020-9-23 19:30:00Select[IntegerPartitions[1000, 2], And @@ PrimeQ@# &] // Length
IntegerPartitions[1000, 2, Prime[Range[PrimePi[1000]]]] // Length

7

Threads

349

Posts

2809

Credits

Credits
2809

Show all posts

睡神 Posted at 2024-3-12 23:05:46
Last edited by 睡神 at 2024-3-13 21:18:00找到一个效率更高的代码
pr[n_]:=DeleteCases[If[PrimeQ[#] && PrimeQ[n-#], {#,n-#},0] & /@Range[3, IntegerPart[n/2], 2], 0]
m = 1000000;
Length[pr[m]]
运行时间:约1秒半钟
而楼上的运行时间需要约16分钟
除了不懂,就是装懂

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2024-3-13 17:42:12
  1. pr[n_]:=Select[Range[3,Floor[n/2],2],PrimeQ[#]&&PrimeQ[n-#]&];
  2. m=1000000;
  3. Length[pr[m]]
Copy the Code

Out[]= 5402

7

Threads

349

Posts

2809

Credits

Credits
2809

Show all posts

睡神 Posted at 2024-3-13 21:17:25
hbghlyj 发表于 2024-3-13 17:42
Compile

https://reference.wolfram.com/language/ref/ParallelMap.html
是不是哪里出错了?
图片1.png

Comment

什么情况?运行了半个多小时,还在运算中。。。  Posted at 2024-3-13 21:34
除了不懂,就是装懂

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2024-3-13 21:58:14
睡神 发表于 2024-3-13 13:17
是不是哪里出错了?
抱歉,我的意思是用ParallelMap替换/@,我的代码错误,已更正

7

Threads

349

Posts

2809

Credits

Credits
2809

Show all posts

睡神 Posted at 2024-3-13 22:01:53
hbghlyj 发表于 2024-3-13 21:58
抱歉,我的意思是用ParallelMap替换/@,我的代码错误,已更正
😂😂😂
一个钟了,还在运算,我只能重启电脑了...
除了不懂,就是装懂

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2024-3-13 22:07:22
hbghlyj 发表于 2024-3-13 13:58
抱歉,我的意思是用ParallelMap替换/@,我的代码错误,已更正

文档说 ParallelMap 比 Map 更快。
  1. pr[n_]:=DeleteCases[ParallelMap[If[PrimeQ[#]&&PrimeQ[n-#],{#,n-#},0]&,Range[3,IntegerPart[n/2],2]],0]
  2. m=1000000;
  3. Length[pr[m]]
Copy the Code

7

Threads

349

Posts

2809

Credits

Credits
2809

Show all posts

睡神 Posted at 2024-3-13 22:15:06
为啥第一个我运行时是这样子的?电脑问题,还是版本问题?而第二个运算效率不算高,用时3分多钟
图片1.png
除了不懂,就是装懂

7

Threads

349

Posts

2809

Credits

Credits
2809

Show all posts

睡神 Posted at 2024-3-13 22:18:05
hbghlyj 发表于 2024-3-13 22:07
文档说 ParallelMap 比 Map 更快。
是不是我的版本问题?
图片1.png

Comment

额~现在才出结果...  Posted at 2024-3-13 22:20
除了不懂,就是装懂

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2024-3-13 22:43:33
睡神 发表于 2024-3-13 14:18
是不是我的版本问题?
有没有可能pr没清除
Clear[pr]

7

Threads

349

Posts

2809

Credits

Credits
2809

Show all posts

睡神 Posted at 2024-3-13 23:26:06
hbghlyj 发表于 2024-3-13 22:43
有没有可能pr没清除
Clear[pr]
对的,没Clean[pr]。代码如下:
Clear[pr]
pr[n_] := DeleteCases[ParallelMap[If[PrimeQ[#] && PrimeQ[n - #], {#, n - #}, 0] &, Range[3, IntegerPart[n/2], 2]], 0]
m = 10000000;
Length[pr[m]]
约3.45s出结果,我的那个约5.45s出结果,效率提升了很多

Comment

m=100000000时,效率得到显著的提高,51s→22.45s  Posted at 2024-3-13 23:31
除了不懂,就是装懂

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2024-3-14 00:09:28
睡神 发表于 2024-3-13 15:26
对的,没Clean[pr]。
是不是拼写错误,好像是Clear[]而不是Clean[]

Comment

是的,第一句拼写错了,后面代码拼写没错...原谅我的英语太烂...  Posted at 2024-3-14 01:37

手机版Mobile version|Leisure Math Forum

2025-4-20 22:06 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list