In Mathematica I Need Help

  • Mathematica
  • Thread starter 76Ahmad
  • Start date
  • #1
48
0

Main Question or Discussion Point

Hi all, a quistion about how to add the cofficients list to gether????
here is an example:

For[m = 2, m ≤ 4 , m++,
For[p = 2, p ≤ m^2, p++,
If[PrimeQ[p], Print[CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x]]]]]

{1,0,1}
{1,1,1}

{1,1,1,1}
{1,0,0,2}
{1,2,3,4}
{1,4,3,6}

{1,0,0,0,1}
{1,2,0,2,1}
{1,1,1,1,1}
{1,3,6,3,1}
{1,7,6,7,1}
{1,9,6,9,1}


How to add every grup of cofficients together?
the reuslt should be like that:

{2,1,2}

{4,7,7,13}

{6,22,19,22,6}

please help :))
 

Answers and Replies

  • #2
1,069
29
In[4]:=For[m=2,m≤4,m++,
Print[
Apply[Plus,
Reap[
For[p=2,p≤m^2,p++,
If[PrimeQ[p],Sow[CoefficientList[PolynomialMod[Expand[(1-x)^m],p],x]]]
]
][[2,1]]
]
]
]

From In[4]:={2,1,2}
From In[4]:={4,7,7,13}
From In[4]:={6,22,19,22,6}
 
  • #3
313
1
You don't really want to use Print[] to extract you data since it's not easily accessed after that.

The way to do what you want with a minimal modification of your code would to be something like

Code:
For[m = 2, m <= 4, m++,
 For[p = 2, p <= m^2, p++,
  If[PrimeQ[p], Print[data[m, p] = 
     CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x]], 
   data[m, p] = 0]]]
Table[Sum[data[m, p], {p, 2, m^2}], {m, 2, 4}]
which returns
Code:
{{2, 1, 2}, {4, 7, 7, 13}, {6, 22, 19, 22, 6}}
But you really should avoid using explicit Do's, For's, etc... as much as possible.

Something like

Code:
Table[Sum[
    CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x],
    {p, Select[Range[2, m^2], PrimeQ]}], 
  {m, 2, 4}]
might be a better way of getting the result...
 

Related Threads on In Mathematica I Need Help

Replies
7
Views
2K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
4
Views
2K
Replies
2
Views
3K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
9
Views
2K
Top