For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > March 2006 > Writing prime factor decomposision in conventional form









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Writing prime factor decomposision in conventional form
Dr. Wolfgang Hintze

2006-03-26, 8:04 am


How can I get a conventional form output for the prime number
decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?

I have found some (rather cumbersome) method but it leaves the final
expression in "" when I put it into input format in order to transfer it
in simple text form to another application.

I'm sure that there is a simple way to achieve the goal.

Any idea is greatly appreciated.

Regards,
Wolfgang

Jean-Marc Gulliet

2006-03-27, 7:08 pm

Dr. Wolfgang Hintze wrote:
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang
>

You could try something like this:

In[1]:=
conventionalForm[n_Integer] :=
Times @@ MapThread[HoldForm[#1^#2] & ,
Transpose[FactorInteger[n]]]

In[2]:=
conventionalForm[468]

Out[2]=
2 2 1
2 3 13

In[3]:=
ReleaseHold[%]

Out[3]=
468

Best regards,
JM

Carl K. Woll

2006-03-27, 7:08 pm

Dr. Wolfgang Hintze wrote:
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang


A couple of methods that might help:

Times @@ Superscript @@@ FactorInteger[40]

or

Times @@ (HoldForm[Power[##]]&) @@@ FactorInteger[40]

Unfortunately, straightforward conversion of the output of these methods
into InputForm will probably not produce the simple text form you are
looking for. What is the simple text form of these outputs that you desire?

Carl Woll
Wolfram Research

Selwyn Hollis

2006-03-27, 7:08 pm

On Mar 26, 2006, at 5:44 AM, Dr. Wolfgang Hintze wrote:

>
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to
> transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang
>


Wolfgang,

Try this:

PrimeFactorization[i_Integer] :=
With[{q = Apply[HoldForm[#1^#2]&,#]& /@
FactorInteger[i]}, Times @@ q]

PrimeFactorization[3104248]

ReleaseHold[%]



Regards,

Selwyn Hollis


Andrzej Kozlowski

2006-03-27, 7:08 pm


On 26 Mar 2006, at 12:44, Dr. Wolfgang Hintze wrote:

>
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to
> transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang
>



Try this:

PrimeDecomposition[n_] := (Times @@ (
InputForm[Power[##]] & @@@ (Map[TraditionalForm, FactorInteger[n],
{2}])))

Now


PrimeDecomposition[40]


2^3 5^1

and so on. I am sure one can force first powers to be rendered as in
your post but I don't think it is worth the effort ;-)

Andrzej Kozlowski

jackgoldberg@comcast.net

2006-03-27, 7:08 pm

Hello,

David B. Wagner addressed this issue a long time ago. He used to be an active participant here but he is no longer. In his book Power Programming with Mathematica, The Kernel, on page 277 is the answer to your question.
I vaguely remember (could be wrong here) that he also wrote a package with a lot more functionality and published it. Others may know more about it.

Jack


-------------- Original message ----------------------
From: "Dr. Wolfgang Hintze" <weh@snafu.de>
>
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang
>



david_tang

2006-03-27, 7:08 pm

you can try these:

define
f1 = (# /. {x_?NumberQ, y_} :> HoldForm[x^y]) &
and you will get the form as you typed.

or
f 2= (# /. {x_?NumberQ, y_} :> x^y) &
and you will get the result of x^y

the reslut will be:
f1[{{2,3},{5,1}}] =>{2^3,5}
f2[{{2,3},{5,1}}] =>{8,5}

good luck!

dh

2006-03-27, 7:08 pm

Hi Wolfgang,

use a String and OutputForm. E.g.:

(If[#[[2]] == 1, #[[1]], StringForm["``^``", #[[1]], #[[2]]]]) & /@ {{2,
3}, {5, 1}}

this yields:

{2^3, 5}

Daniel

Dr. Wolfgang Hintze wrote:
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang
>


David Park

2006-03-27, 7:08 pm

Wolfgang,

I'm sure something like this is somewhere in the Help.

FactorForm[n_Integer] := Times @@
((HoldForm[#1^#2] & ) @@ #1 & ) /@ FactorInteger[n]

FactorForm[40]

FactorForm[425623016]

There is a certain advantage to explicitly displaying the '1' superscripts
here because it keep all the prime factors in sort order.

David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/


From: Dr. Wolfgang Hintze [mailto:weh@snafu.de]


How can I get a conventional form output for the prime number
decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?

I have found some (rather cumbersome) method but it leaves the final
expression in "" when I put it into input format in order to transfer it
in simple text form to another application.

I'm sure that there is a simple way to achieve the goal.

Any idea is greatly appreciated.

Regards,
Wolfgang


Paul Abbott

2006-03-27, 7:08 pm

In article <e05ri1$3ru$1@smc.vnet.net>,
"Dr. Wolfgang Hintze" <weh@snafu.de> wrote:

> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.


This is a FAQ, as you will see if you do a search on

FactorInteger format

at http://groups.google.com/group/comp...th.mathematica. The
main idea is to use HoldForm and ReleaseHold. Here is one solution:

FactorInteger[238500]

Times @@ Apply[Power, Map[HoldForm, %, {2}], {1}]

ReleaseHold[%]

Cheers,
Paul

________________________________________
_______________________________
Paul Abbott Phone: 61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
AUSTRALIA http://physics.uwa.edu.au/~paul

mike_in_england2006@yahoo.co.uk

2006-03-27, 7:08 pm

Hello Wolfgang

I found the folllowing code at
http://mathworld.wolfram.com/PrimeFactorization.html

SetAttributes[FactorForm, Listable];

FactorForm[n_?NumberQ, opts___] := Times @@ (HoldForm[Power[##]] & @@@
\
FactorInteger[n, opts])

Seems to do what you want.
Regards,
Mike


Dr. Wolfgang Hintze wrote:

> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang


Peter Pein

2006-03-29, 8:05 am

Dr. Wolfgang Hintze schrieb:
> How can I get a conventional form output for the prime number
> decomposition, e.g. {{2,3},{5,1}} as 2^3 5 ?
>
> I have found some (rather cumbersome) method but it leaves the final
> expression in "" when I put it into input format in order to transfer it
> in simple text form to another application.
>
> I'm sure that there is a simple way to achieve the goal.
>
> Any idea is greatly appreciated.
>
> Regards,
> Wolfgang
>

Hi Wolfgang,

another possibility is to look into the documentation (this time it is hard to find):
http://documents.wolfram.com/mathem.../MakeBoxes.html

Peter

Cca

2006-03-29, 8:05 am

Wolfgang,

Much to my surprise, no one seems to have noticed that a solution
(surely due to Robby Villegas) is presented in the HelpBrowser under
MakeBoxes.

Below I offer some little variations. (Notice that the outputs
are evaluatable.)
------------------------------------------------------
FactoredInteger[0]=0;

FactoredInteger[1]=1;

FactoredInteger[n_Integer /; n > 1, multsign_: " "] :=
Module[{facs}, facs = FactorInteger[n];
DisplayForm@
Replace[Replace[Hold[Evaluate[facs]], {p_, a_} :> p^a, {2}],
Hold[{pots___}] :>
If[Length[facs] === 1, MakeBoxes[pots, StandardForm],
MakeBoxes[Times[pots],
StandardForm] /. " "-> multsign]]]

FactoredInteger[n_/;n<0]:=-FactoredInteger[-n]

Examples:

FactoredInteger[238500]

FactoredInteger[238500,"×"]

FactoredInteger[238500,"*"]

CenterDot=Times;
FactoredInteger[238500,"ˇ"]
------------------------------------------------------
FactoredInteger2[n_Integer /; n > 1, multsign_: " "] :=
With[{facs = FactorInteger[n]}, DisplayForm@
Replace[Replace[Hold[facs], {p_, a_} :> p^a, {2}],
Hold[{pots___}] :>
If[Length[facs] === 1,
MakeBoxes[pots,
StandardForm] /. {SuperscriptBox[a_, b_] :>
RowBox[{a, "^", b}], " " -> multsign},
MakeBoxes[Times[pots],
StandardForm] /. {SuperscriptBox[a_, b_] :>
RowBox[{a, "^", b}], " " -> multsign}]]]
Examples:

In[10]:=
FactoredInteger2[238500,"*"]

Out[10]//DisplayForm=
2^2*3^2*5^3*53^1

In[11]:=
FactoredInteger2[238500,"×"]

Out[11]//DisplayForm=
2^2×3^2×5^3×53^1


Carlos César de Araújo
Gregos & Troianos Educacional
www.gregosetroianos.mat.br
Belo Horizonte, MG, Brasil
(31) 3283-1122

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com