1
$\begingroup$

Wolfram Mathworld (http://mathworld.wolfram.com/SumofPrimeFactors.html) describes a function sopfr(n), the sum of prime factors, which I currently need. This code doesn't work when I insert it in Mathematica. I want a version of this code that does not do repetition. So, e.g., the output for $20=2^25$ would be $2+5$ and not $2+2+5$.

Given $n$, and $j\le n,$ I am requesting a code for the that gives output $$\sum_{p\le n, p\,\text{prime factor of } j}p\,.$$

when the input is $n,j$. So here I do not get the entire sum of prime factors, only those bounded by some prescribed $n$.

$\endgroup$

1 Answer 1

7
$\begingroup$

Note: I think you meant "bounded by some prescribed j", not by some n.

FactorInteger[11!]

(* {{2, 8}, {3, 4}, {5, 2}, {7, 1}, {11, 1}} *)

Map[First, FactorInteger[11!]]

(* {2, 3, 5, 7, 11} *)

sopfr[n_, j_] := Select[Map[First, FactorInteger[n]], # < j &];
sopfr[11!, 8]

(* {2, 3, 5, 7} *)

sopfr[n_, j_] := Total[Select[Map[First, FactorInteger[n]], # < j &]];
sopfr[11!, 8]

(* 17 *)

Try to understand the thinking that must have been behind each step of this as it was written. That you will be able to make use of for the next problem.

$\endgroup$
3
  • $\begingroup$ You are correct, there's no need to even mention $n$ here. I am asking for a sum of the prime divisors of $j$. In this case, 3,5,7 don't divide $j=8$, so i wouldn't include those in the sum. $\endgroup$ Commented Jul 17, 2017 at 22:14
  • $\begingroup$ I think I've found what I want: sopfr[n_] := Total[Select[Map[First, FactorInteger[n]]]];Total[sopfr[10]]. When I don't insert total, it just gives a table, but the Total gives the desired sum. $\endgroup$ Commented Jul 17, 2017 at 22:18
  • $\begingroup$ @The Substitute I'm a little confused. I thought your question was "Give me the total of the prime factors of n that are less than j." If you are trying to modify my example to eliminate the "less than j" part then perhaps this will do: sopfr[j_]:=Total[Map[First,FactorInteger[j]]];sopfr[10] $\endgroup$
    – Bill
    Commented Jul 17, 2017 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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