Re: Peculiar behaviour of Mathematica code

• To: mathgroup at smc.vnet.net
• Subject: [mg57961] Re: Peculiar behaviour of Mathematica code
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Tue, 14 Jun 2005 05:10:25 -0400 (EDT)
• Organization: The Open University, Milton Keynes, England
• References: <d8jlau\$srs\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Tony King wrote:
> 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.
>
> 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
>
> Thank you
>
> Tony
>
Hi Tony,

What you could do is to add a new conditional definition to each of the
function rather than modifying the existing code. For example

In[1]:=
SemiperfectQ[(n_Integer)?PrimeQ] := False

In[2]:=
SemiperfectQ[(n_Integer)?Positive] :=
Module[{d = Most[Divisors[n]]},
(n == Plus @@ #1 & ) /@ Or @@ Rest[Subsets[d]]]

In[3]:=
SemiperfectQ[4]

Out[3]=
False

In[4]:=
SemiperfectQ[5]

Out[4]=
False

In[5]:=
SemiperfectQ[6]

Out[5]=
True

Beware that the definition with a test for primality must be executed
before the definition with a test for positivity (otherwise, since a
prime is always positive, the definition that tests whether the integer
is positive will be called first and always applied leaving no chance to
the second definition that tests primality to be called).

Hope this helps,
/J.M.

```

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