-5
$\begingroup$

I have a homework problem about perfect numbers. I simply did it, but I need to use Divisors, Most, Plus, Apply and AppendTo commands.

Here is my work; https://i.hizliresim.com/Mv5nL7.jpg

I am awaiting your answers.

$\endgroup$
4
  • 5
    $\begingroup$ We are awaiting your code to be inserted into your question in a form that we can copy and paste into Mathematica. $\endgroup$
    – m_goldberg
    Jun 2, 2017 at 1:28
  • 1
    $\begingroup$ Duplicate of this question? $\endgroup$ Jun 2, 2017 at 1:59
  • 1
    $\begingroup$ Possible duplicate of Perfect numbers $\endgroup$
    – garej
    Jun 2, 2017 at 4:01
  • $\begingroup$ Select[Range[10^4], Total[Divisors@#] == 2 # &] Divisors[6] Most[{1, 2, 3, 6}] Apply[Plus, Divisors[6]] $\endgroup$
    – nomerhamet
    Jun 2, 2017 at 18:03

3 Answers 3

3
$\begingroup$

You can avoid all the functions you mentioned.

src = " #include \"WolframLibrary.h\"

  mint isPerfect(mint num){
      mint div_sum = 1;
      for (mint i=2; i*i<=num; i++)
          if (num%i==0) div_sum += i + num/i;
      if (div_sum - num) return 0; return 1;
  }


  DLLEXPORT int getPerfectNumber(WolframLibraryData libData, mint \
Argc, MArgument *Args, MArgument Res) {
      mint limit = MArgument_getInteger(Args[0]), res[100], res_count \
= 0;
      for (int i = 2; i <=limit; i+=2)
          if (isPerfect(i)) res[res_count++] = i;
      MTensor out;
      mint out_dims[1];
      out_dims[0]=res_count;
      mint* out_data;
      int err;
      err = libData->MTensor_new(MType_Integer, 1, out_dims, &out);
      out_data = libData->MTensor_getIntegerData(out);
      for (int i = 0; i < res_count; ++i)
          out_data[i] = res[i];
      MArgument_setMTensor(Res,out);
      return LIBRARY_NO_ERROR;
  }";
Needs["CCompilerDriver`"]
lib = CreateLibrary[src, "getPerfectNumber"];
getPerfectNumber = 
  LibraryFunctionLoad[lib, 
   "getPerfectNumber", {Integer}, {Integer, 1}];
getPerfectNumber[10000]
$\endgroup$
1
  • $\begingroup$ thank you but i need the all functions. $\endgroup$
    – nomerhamet
    Jun 2, 2017 at 18:04
0
$\begingroup$
PerfectTest[n_Integer] := Total[Divisors[n]] == 2 n;

Select[Range[1000], PerfectTest]

(*

{6, 28, 496}

*)

$\endgroup$
1
  • $\begingroup$ thank you, but i did it already. I need to use Divisors, Most, Plus, Apply and AppendTo commands. $\endgroup$
    – nomerhamet
    Jun 2, 2017 at 18:06
0
$\begingroup$
f[n_] := Module[{p = MersennePrimeExponent[n]}, 2^(p - 1) (2^p - 1)]
j = 1; pn = {};
While[f[j] < 10000, AppendTo[pn, f[j]]; j++]
pn
$\endgroup$
1
  • $\begingroup$ Is it definition of Divisors? $\endgroup$
    – nomerhamet
    Jun 2, 2017 at 18:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.