MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Peculiar behaviour of Mathematica code

  • To: mathgroup at
  • Subject: [mg57964] Re: Peculiar behaviour of Mathematica code
  • From: "Carl K. Woll" <carlw at>
  • Date: Tue, 14 Jun 2005 05:10:29 -0400 (EDT)
  • Organization: University of Washington
  • References: <d8jlau$srs$>
  • Sender: owner-wri-mathgroup at

"Tony King" <mathstutoring at> wrote in message 
news:d8jlau$srs$1 at
>I have some Mathematica code which I borrowed from the Divisors package.
> This code should return True if its argument is semiperfect and False if 
> it
> is not.
> Here is the code
> SemiperfectQ[n_Integer?Positive]:=Module[{d=Most[Divisors[n]]},
>    n==Plus@@#&/@(Or@@Rest[Subsets[d]])
> ]
> This seems to work fine if the argument is composite. However, if the 
> input
> is prime it returns {False} rather than False.

Here is a different approach which is generally faster:

SemiperfectQ[n_Integer?Positive] :=
Block[{x}, Coefficient[Expand[Times @@ (1 + x^Most[Divisors[n]])], x^n] > 0]

Both approaches essentially compute the power set of the divisors, and so 
the time grows exponentially with the number of divisors. If you are 
interested in numbers with a lot of divisors, you may wish to investigate 
algorithms which don't compute the power set (e.g. backtracking).

> Similarly the code for returning True if a number is weird behaves in the
> same way
> WeirdQ[n_Integer?Positive]:=DivisorSigma[1,n]>2n&&
>            n!=Plus@@#&/@(And@@Rest[Subsets[Most[Divisors[n]]]])
> Any ideas how these codes could be modified to return False (rather than
> {False}) for prime inputs

Once you have SemiperfectQ working, then it's simple:

WeirdQ[n_Integer?Positive]:=DivisorSigma[1,n]>2n && !SemiperfectQ[n]

> Thank you
> Tony

Carl Woll
Wolfram Research 

  • Prev by Date: Re: Q: Thread behaviour
  • Next by Date: Re: Exporting mathematica equations into MathType
  • Previous by thread: Peculiar behaviour of Mathematica code
  • Next by thread: Re: Peculiar behaviour of Mathematica code