FactorInteger 的替代形式? (数学)

发布于 2024-07-17 02:51:41 字数 467 浏览 6 评论 0原文

在 Mathematica 中

a = FactorInteger[44420069694]

赋值

{{2, 1}, {3, 1}, {7, 1}, {11, 2}, {13, 1}, {23, 2}, {31, 1}, {41, 1}}

给 a。 现在,我希望扩展每个列表,而不是带有指数的因子。 上面的因式分解将变成

{2, 3, 7, 11, 11, 13, 23, 23, 31, 41}

我编写的以下函数:

b = {}; Do[Do[b = Append[b, a[[i]][[1]]], {a[[i]][[2]]}], {i, Length[a]}]

但如果你问我它看起来很丑陋。 肯定有一种更简洁的方法来实现这一目标吗?

In Mathematica

a = FactorInteger[44420069694]

assigns

{{2, 1}, {3, 1}, {7, 1}, {11, 2}, {13, 1}, {23, 2}, {31, 1}, {41, 1}}

to a. Now instead of the factors with their exponents I would like each of those lists expanded. The above factorization would then become

{2, 3, 7, 11, 11, 13, 23, 23, 31, 41}

I wrote the following function:

b = {}; Do[Do[b = Append[b, a[[i]][[1]]], {a[[i]][[2]]}], {i, Length[a]}]

but if you ask me it looks fugly. There sure must be a neater way to do achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

‘画卷フ 2024-07-24 02:51:41

是的,例如:

Flatten[Map[Table[#[[1]], {#[[2]]}] &, a]]

Yes, for example:

Flatten[Map[Table[#[[1]], {#[[2]]}] &, a]]
追风人 2024-07-24 02:51:41

Mathematica 6 或更高版本中的另一种方法。

In:= Flatten[ConstantArray @@@ a]

Out={2, 3, 7, 11, 11, 13, 23, 23, 31, 41}

更短:

Join @@ ConstantArray @@@ a


发布方法的速度比较

使用这些函数(按照发布的顺序):

zvrba = Flatten[Map[Table[#[[1]], {#[[2]]}] &, #]] &;
dreeves = Sequence @@ Table[#1, {#2}] & @@@ # &;
gdelfino = Flatten[# /. {p_, n_} :> Table[p, {n}]] &;
mrwizard = Join @@ ConstantArray @@@ # &;
sasha = Function[{p, e}, Array[p &, e, 1, Sequence]] @@@ # &;

并分别为它们分配字母 Z、D、G、M、S ,这是其效率的时序图。

首先,为了增加输入中的列表数量:

在此处输入图像描述

其次,为了增加指数(重复长度)每个列表:

在此处输入图像描述

请注意,这些图表是对数的。 越低越好。

Yet another way in Mathematica 6 or later.

In:= Flatten[ConstantArray @@@ a]

Out={2, 3, 7, 11, 11, 13, 23, 23, 31, 41}

even shorter:

Join @@ ConstantArray @@@ a


A speed comparison of methods posted

Using the these functions (in the order they were posted):

zvrba = Flatten[Map[Table[#[[1]], {#[[2]]}] &, #]] &;
dreeves = Sequence @@ Table[#1, {#2}] & @@@ # &;
gdelfino = Flatten[# /. {p_, n_} :> Table[p, {n}]] &;
mrwizard = Join @@ ConstantArray @@@ # &;
sasha = Function[{p, e}, Array[p &, e, 1, Sequence]] @@@ # &;

and assigning them the letters Z, D, G, M, S respectively, here are Timing charts of their efficiency.

First, for increasing number of lists in the input:

enter image description here

Second, for increasing exponent (length of repetition) in each list:

enter image description here

Note that these charts are logarithmic. Lower is better.

初心 2024-07-24 02:51:41

这是另一种方法:

rptseq[x_, n_] := Sequence @@ Table[x, {n}]
rptseq @@@ a

可以用 lambda 函数将其压缩为:

Sequence @@ Table[#1, {#2}] & @@@ a

如果您喜欢这种事情,zvrba 的答案也可以压缩一点:(

Flatten[Table[#1, {#2}]& @@@ a]

现在我看看它,我想我的版本是zvrba 的非常小的变体。)

Here's another way to do it:

rptseq[x_, n_] := Sequence @@ Table[x, {n}]
rptseq @@@ a

Which can be condensed with a lambda function to:

Sequence @@ Table[#1, {#2}] & @@@ a

zvrba's answer can also be condensed a bit, if you're into that sort of thing:

Flatten[Table[#1, {#2}]& @@@ a]

(Now that I look at that, I guess my version is a very minor variant on zvrba's.)

饭团 2024-07-24 02:51:41

您还可以使用:

a /. {p_, n_} -> Table[p, {n}] // Flatten

更新2017/10/18

正如科里·沃克(Cory Walker)指出的那样,“在两个不同素因数的情况下”,我上面的答案失败了。 本次更新修复了这个问题:

a /. {p_Integer, n_Integer} -> Table[p, {n}] // Flatten

请注意,Mr Wizard 所做的基准测试是针对本次更新之前的原始版本进行的。

You could also use:

a /. {p_, n_} -> Table[p, {n}] // Flatten

UPDATE 2017/10/18:

My answer above fails "in the case of two distinct prime factors" as pointed out by Cory Walker. This update fixes it:

a /. {p_Integer, n_Integer} -> Table[p, {n}] // Flatten

notice that the benchmark done by Mr Wizard was done with the original version before this update.

廻憶裏菂餘溫 2024-07-24 02:51:41

也可以使用数组来处理答案。 这是执行此操作的简短代码:

In[11]:= PrimeFactorInteger[i_Integer] := 
 Function[{p, e}, Array[p &, e, 1, Sequence]] @@@ FactorInteger[i]

In[12]:= PrimeFactorInteger[2^3 3^2 5]

Out[12]= {2, 2, 2, 3, 3, 5}

One can also use Array to process the answer. Here is a short code doing this:

In[11]:= PrimeFactorInteger[i_Integer] := 
 Function[{p, e}, Array[p &, e, 1, Sequence]] @@@ FactorInteger[i]

In[12]:= PrimeFactorInteger[2^3 3^2 5]

Out[12]= {2, 2, 2, 3, 3, 5}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文