Equal prime and composite sums: Difference between revisions

m
(Added Algol 68)
 
(5 intermediate revisions by 4 users not shown)
Line 33:
 
=={{header|ALGOL 68}}==
{{Trans|XPL0|With a fewcoup[le of tweaks}}
<syntaxhighlight lang="algol68">
BEGIN # find n and m where the sums of the first n primes and first m #
Line 68:
m +:= 1
FI;
IF sum p /= sum c THEN TRUE # continue the loop #
ELSE
print( ( whole( sum p, -16 ), whole( n, -10 ), whole( m, -11 ), newline ) );
count +:= 1;
IF count >=< 8 THEN FALSE # loop stops here #
ELSE
WHILE is prime( num c +:= 1 ) DO SKIP OD;
sum c +:= num c;
m +:= 1;
TRUE # continue the loop #
FI
FI;
count < 8
DO SKIP OD
END
Line 472 ⟶ 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 819 ⟶ 872:
9646383703961 1131142 4229425
209456854921713 5012372 19786181</pre>
 
=={{header|Lua}}==
{{Trans|XPL0|with a couple of tweaks}}
<syntaxhighlight lang="lua">
do --[[ find n and m where the sums of the first n primes and first m
composites where the sums are equal
--]]
 
local function isPrime( n ) -- returns TRUE if n is prime
if n <= 2 then return n == 2
elseif n % 2 == 0 then return false
else local prime, i, r = true, 1, math.floor( math.sqrt( n ) )
repeat
i = i + 2
if i <= r then
prime = n % i ~= 0
end
until i > r or not prime
return prime
end
end
 
local count, n, m, sumP, sumC, numP, numC = 0, 2, 1, 5, 4, 3, 4
io.write( " sum primes composites\n" )
 
repeat
if sumC > sumP then
repeat numP = numP + 2 until isPrime( numP )
sumP = sumP + numP
n = n + 1
end
if sumP > sumC then
repeat numC = numC + 1 until not isPrime( numC )
sumC = sumC + numC
m = m + 1
end
if sumP == sumC then
io.write( string.format( "%14d", sumP ), string.format( "%10d", n ), string.format( "%11d", m ), "\n" )
count = count + 1;
if count < 8 then
repeat numC = numC + 1 until not isPrime( numC )
sumC = sumC + numC
m = m + 1
end
end
until count >= 8
end
</syntaxhighlight>
{{out}}
<pre>
sum primes composites
10 3 2
1988 33 51
14697 80 147
83292 175 361
1503397 660 1582
18859052 2143 5699
93952013 4556 12821
89171409882 118785 403341
</pre>
 
=={{header|Nim}}==
Line 1,016 ⟶ 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}}==
Line 1,169 ⟶ 1,320:
=={{header|Wren}}==
Takes around 2 minutes, which is respectable for Wren, but uses a lot of memory.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./sort" for Find
import "./fmt" for Fmt
 
var limit = 4 * 1e8
2,023

edits