Equal prime and composite sums: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
(Added Python implementation for Equal prime and composite sums task) |
||
Line 1,074: | Line 1,074: | ||
</pre> |
</pre> |
||
The next value in the series is beyond an 80 bit float, and I suspect this is one of those sort of tasks where gmp, or perhaps I should rather say over a billion invocations of the Phix interface to it, might not shine quite so brightly. |
The next value in the series is beyond an 80 bit float, and I suspect this is one of those sort of tasks where gmp, or perhaps I should rather say over a billion invocations of the Phix interface to it, might not shine quite so brightly. |
||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python3"> |
|||
# equal_prime_comp_sums.py by Xing216 |
|||
import math |
|||
import numpy |
|||
def prime_composites(upto=50000): |
|||
nums = numpy.arange(2,upto+1) |
|||
primes=numpy.arange(3,upto+1,2) |
|||
isprime=numpy.ones((upto-1)//2,dtype=bool) |
|||
for factor in primes[:int(math.sqrt(upto))//2]: |
|||
if isprime[(factor-2)//2]: isprime[(factor*3-2)//2::factor]=0 |
|||
primes = numpy.insert(primes[isprime],0,2) |
|||
intersect = nums[numpy.in1d(nums, primes)] |
|||
mask1 = numpy.searchsorted(nums,intersect) |
|||
composites = numpy.delete(nums,mask1) |
|||
return primes, composites |
|||
primes, composites = prime_composites() |
|||
cum_primes = numpy.cumsum(primes) |
|||
cum_composites = numpy.cumsum(composites) |
|||
print("Sum | Prime Index | Composite Index") |
|||
print("------------------------------------------") |
|||
for idx, num in enumerate(cum_primes): |
|||
if num in cum_composites: |
|||
print(f"{num:10,} | {idx+1:11,} | {numpy.where(cum_composites == num)[0][0]+1:15,}") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Sum | Prime Index | Composite Index |
|||
------------------------------------------ |
|||
10 | 3 | 2 |
|||
1,988 | 33 | 51 |
|||
14,697 | 80 | 147 |
|||
83,292 | 175 | 361 |
|||
1,503,397 | 660 | 1,582 |
|||
18,859,052 | 2,143 | 5,699 |
|||
93,952,013 | 4,556 | 12,821 |
|||
</pre> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |