Message Boards Message Boards

0
|
776 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Is there a way to show the factors of a Perfect Number expressed as a sum rather than a product

Posted 2 months ago

Hi,

Normally, FactorInteger[] function outputs all the prime factors of a number as a list composed of the factors and their exponents, but I would like to see the output expressed as the sum of all the proper factors (all factors less than the value). Here are 3 examples that should help illustrate what I'm looking for:

  1. FactorInteger[28] output would show {{2, 2}, {7, 1}} but I would like to see it expressed as:

     28 = 1 + 2 + 4 + 7 + 14
    
  2. FactorInteger[496] outuput would show {{2, 4}, {31, 1}}, but I would like to see it expressed as:

    496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
    
  3. FactorInteger[8128] output would show {{2,6}, {127, 1}}, but I would like to see it expressed as:

    8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
    

I also just came across the Divisors[] function that creates a list of all possible divisors of a given value, which is sorta close, but would really like to see the values expressed as a sum rather than a list of values. I also found the Drop[] function that would remove the last element so that it's easy to see the entire list of proper divisors, but really would like to express it as a sum rather than the list. E.G.

In[1]:= Drop[Divisors[8128], -1]

Out[1]= {1, 2, 4, 8, 16, 32, 64, 127, 254, 508, 1016, 2032, 4064}

So, how would I transform this to something like

8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064

Any suggestions?? Thanks very much...

-bob

POSTED BY: Bob Freeman
4 Replies

Here's a notebook with CellProlog and CellEpilog set up to automatically print perfect numbers in expanded form. One advantage is that Out[$Line] (or %) is equal to the actual integer and not some held or inactive form. And if you copy the output (either Copy/Paste or Cmd-Opt-L right below the output on a Mac), you get what you see (no hidden HoldForm[] and Defer[] is automatically stripped).

Note that this approach need not be applied via CellProlog. One can simply execute the following in a notebook:

$PrePrint = 
   Function[expr, 
     If[TrueQ[$expandPerfectNumbers] && PerfectNumberQ[expr],
       Defer[Plus[##]] & @@ Most@Divisors@expr, expr]];

In this case it will be set and apply to all notebooks until $PrePrint is unset with the following:

$PrePrint =.

Note that CellEpilog does the unsetting after every execution, so in effect, the expansion of perfect numbers happens only in the "Perfect Number Notebook" and not in other open notebooks. Also the SetOptions[EvaluationNotebook[],...] need only be executed once; when the notebook is saved, the options become persistent until explicitly reset. Note also that when CellEpilog unsets $PrePrint for all notebooks. A more careful implementation would use CellProlog to save the current value of $PrePrint and CellEpilog to restore it, if it already has a value. One might even try to compose the two, so that the user's $PrePrint would stay in effect in "Perfect Number Notebook." (I think I wouldn't do this. I would rather assert control over the notebook, so that I could be sure how it would behave.)

POSTED BY: Michael Rogers
Posted 2 months ago

Here's an approach:

PerfectSumForm[n_?PerfectNumberQ] := Inactive[Plus] @@ Most[Divisors[n]];

PerfectSumForm[8128]
(* This will display as
   1+2+4+8+16+32+64+127+254+508+1016+2032+4064
   You can allow the sum to execute with Activate*)

PerfectSumForm[8128] // Activate
(* 8128 *)
POSTED BY: Eric Rimbey
Posted 2 months ago

If all you want to do is to see the result then

Plus@@Map[HoldForm,Drop[Divisors[8128], -1]]

returns this

1+2+4+8+16+32+64+127+254+508+1016+2032+4064

But the appearance of that is different from what is behind the scene. You can use ReleaseHold to turn that into something you can then use in another calculation, but the sum will be immediately calculated when you do that

POSTED BY: Bill Nelson
Posted 2 months ago

Just what the doctor ordered!!! Thanks very much.

POSTED BY: Bob Freeman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract