barry houdini
MrExcel MVP
- Joined
- Mar 23, 2005
- Messages
- 20,825
Hello Fairwinds,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o> </o>
Now that’s what I call a formula, I’ll need to go away and work out exactly how it works.<o></o>
<o> </o>
It prompted me to look again at my suggestion. I had realised that I could limit the MOD divisor range to the square root of N but that didn’t work within my original formula because my “primeness” test for each n of 1 to N was that it would have a single factor (not including 1), i.e. n itself. If I restricted it to SQRT(N) then some primes (those < SQRT(N)) would have no factors, and the others would have 1.<o></o>
<o> </o>
But I can change the formula slightly to take that into account (calculating whether each number needs 1 factor or 0 to be prime).<o></o>
<o> </o>
Having made that fix I realised that MMULT was still limiting the value of N to 5465 (although that restriction doesn’t apply in Excel 2007) so I changed the formula further to only consider odd numbers.<o></o>
<o> </o>
This formula, then, works on values of N up to 10926 (shouldn’t be a problem to go higher in Excel 2007 but I can’t access that version right now).
=SUM((FREQUENCY(ROW(INDIRECT("1:"&INT(A1/2+1.5)))*2+1,(MMULT((MOD(ROW(INDIRECT("2:"&INT(A1/2-0.5)))*2+1,TRANSPOSE(ROW(INDIRECT("2:"&INT(A1^(1/2))))))=0)+0,ROW(INDIRECT("2:"&INT(A1^(1/2))))^0)=(ROW(INDIRECT("2:"&INT(A1/2-0.5)))*2+1<=INT(A1^(1/2)))+0)*ROW(INDIRECT("2:"&INT(A1/2-0.5)))*2+1)=1)+0)
confirmed with CTRL+SHIFT+ENTER<o></o>
<o> </o>
Now that’s what I call a formula, I’ll need to go away and work out exactly how it works.<o></o>
<o> </o>
It prompted me to look again at my suggestion. I had realised that I could limit the MOD divisor range to the square root of N but that didn’t work within my original formula because my “primeness” test for each n of 1 to N was that it would have a single factor (not including 1), i.e. n itself. If I restricted it to SQRT(N) then some primes (those < SQRT(N)) would have no factors, and the others would have 1.<o></o>
<o> </o>
But I can change the formula slightly to take that into account (calculating whether each number needs 1 factor or 0 to be prime).<o></o>
<o> </o>
Having made that fix I realised that MMULT was still limiting the value of N to 5465 (although that restriction doesn’t apply in Excel 2007) so I changed the formula further to only consider odd numbers.<o></o>
<o> </o>
This formula, then, works on values of N up to 10926 (shouldn’t be a problem to go higher in Excel 2007 but I can’t access that version right now).
=SUM((FREQUENCY(ROW(INDIRECT("1:"&INT(A1/2+1.5)))*2+1,(MMULT((MOD(ROW(INDIRECT("2:"&INT(A1/2-0.5)))*2+1,TRANSPOSE(ROW(INDIRECT("2:"&INT(A1^(1/2))))))=0)+0,ROW(INDIRECT("2:"&INT(A1^(1/2))))^0)=(ROW(INDIRECT("2:"&INT(A1/2-0.5)))*2+1<=INT(A1^(1/2)))+0)*ROW(INDIRECT("2:"&INT(A1/2-0.5)))*2+1)=1)+0)
confirmed with CTRL+SHIFT+ENTER<o></o>