Equal prime and composite sums: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 470:
</pre>
 
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
fastfunc isprim num .
if num mod 2 = 0 and num > 2
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
indN = 1 ; indM = 2
numP = 2 ; numC = 4
sumP = 2 ; sumC = 4
#
numfmt 0 11
print " sum primes composites"
repeat
if sumC > sumP
repeat
numP += 1
until isprim numP = 1
.
sumP += numP
indN += 1
.
if sumP > sumC
repeat
numC += 1
until isprim numC = 0
.
sumC += numC
indM += 1
.
if sumP = sumC
print sumP & indN & indM
cnt += 1
if cnt < 8
repeat
numC += 1
until isprim numC = 0
.
sumC += numC
indM += 1
.
.
until cnt >= 8
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,074 ⟶ 1,129:
</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.
=={{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}}==
2,041

edits