Multiplicatively perfect numbers: Difference between revisions

Added Easylang
(→‎{{header|C}}: Optimized and extended to 5 million,)
(Added Easylang)
 
(10 intermediate revisions by 7 users not shown)
Line 99:
Up to 500000 there are 108223 multiplicatively perfect numbers and 108326 semi-primes
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">arraybase 1
limit = 500
dim Divisors(1)
c = 0
 
print "Special numbers under "; limit; ":"
 
for n = 1 to limit
pro = 1
for m = 2 to ceil(n / 2)
if n mod m = 0 then
pro *= m
c += 1
redim Divisors(c)
Divisors[c] = m
end if
next m
ub = Divisors[?]
if n = pro and ub > 2 then
print rjust(string(n), 3); " = "; rjust(string(Divisors[ub-1]), 2); " x "; rjust(string(Divisors[ub]), 3)
end if
next n</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">LET limit = 50
DIM divisors(0)
LET c = 1
 
PRINT "Special numbers under"; limit; ":"
 
FOR n = 1 TO limit
LET pro = 1
FOR m = 2 TO ceil(n/2)
IF remainder(n, m) = 0 THEN
LET pro = pro*m
MAT REDIM divisors(c)
LET divisors(c) = m
LET c = c+1
END IF
NEXT m
LET ub = UBOUND(divisors)
IF n = pro AND ub > 1 THEN PRINT USING "### = ## x ###": n, divisors(ub-1), divisors(ub)
NEXT n
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">Macro Ceil (_x_)
Round(_x_, #PB_Round_Up)
EndMacro
 
OpenConsole()
Define.i limit = 500
Dim Divisors.i(1)
Define.i n, pro, m, c = 0, ub
 
PrintN("Special numbers under" + Str(limit) + ":")
 
For n = 1 To limit
pro = 1
For m = 2 To Ceil(n / 2)
If Mod(n, m) = 0:
pro * m
ReDim Divisors(c) : Divisors(c) = m
c + 1
EndIf
Next m
ub = ArraySize(Divisors())
If n = pro And ub > 1:
PrintN(RSet(Str(n),3) + " = " + RSet(Str(Divisors(ub-1)),2) + " x " + RSet(Str(Divisors(ub)),3))
EndIf
Next n
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">limit = 500
dim Divisors(1)
c = 1
 
print "Special numbers under", limit, ":"
 
for n = 1 to limit
pro = 1
for m = 2 to ceil(n / 2)
if mod(n, m) = 0 then
pro = pro * m
dim Divisors(c) : Divisors(c) = m
c = c + 1
fi
next m
ub = arraysize(Divisors(), 1)
if n = pro and ub > 1 then
print n using "###", " = ", Divisors(ub-1) using "##", " x ", Divisors(ub) using "###"
fi
next n</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
This includes '1' as an MPN. Run time around 3.2.3 seconds.
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
Line 127 ⟶ 237:
if (!(n%i)) {
divs[c++] = i;
if (c > 32) break; // not eligible if has > 2 divisors
j = n / i;
if (j != i) divs[c++] = j;
Line 137 ⟶ 247:
int main() {
int i, d, j, k, t, length, prod;
int divs[54], count = 0, limit = 500, s = 3, c = 3, squares = 1, cubes = 1;
printf("Multiplicatively perfect numbers under %d:\n", limit);
setlocale(LC_NUMERIC, "");
Line 193 ⟶ 303:
Counts under 500,000: MPNs = 108,223 Semi-primes = 108,326
Counts under 5,000,000: MPNs = 978,983 Semi-primes = 979,274
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang=C++>#include <iostream>
#include <vector>
#include <numeric>
 
std::vector<int> divisors( int n ) {
std::vector<int> divisors ;
for ( int i = 1 ; i < n + 1 ; i++ ) {
if ( n % i == 0 )
divisors.push_back( i ) ;
}
return divisors ;
}
 
int main( ) {
std::vector<int> multi_perfect ;
for ( int i = 1 ; i < 501 ; i++ ) {
std::vector<int> divis { divisors( i ) } ;
if ( std::accumulate( divis.begin( ) , divis.end( ) , 1 ,
std::multiplies<int>() ) == (i * i ) )
multi_perfect.push_back( i ) ;
}
std::cout << '(' ;
int count = 1 ;
for ( int i : multi_perfect ) {
std::cout << i << ' ' ;
if ( count % 15 == 0 )
std::cout << '\n' ;
count++ ;
}
std::cout << ")\n" ;
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>
(1 6 8 10 14 15 21 22 26 27 33 34 35 38 39
46 51 55 57 58 62 65 69 74 77 82 85 86 87 91
93 94 95 106 111 115 118 119 122 123 125 129 133 134 141
142 143 145 146 155 158 159 161 166 177 178 183 185 187 194
201 202 203 205 206 209 213 214 215 217 218 219 221 226 235
237 247 249 253 254 259 262 265 267 274 278 287 291 295 298
299 301 302 303 305 309 314 319 321 323 326 327 329 334 335
339 341 343 346 355 358 362 365 371 377 381 382 386 391 393
394 395 398 403 407 411 413 415 417 422 427 437 445 446 447
451 453 454 458 466 469 471 473 478 481 482 485 489 493 497
)
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
for n = 1 to 500
pro = 1
for m = 2 to n div 2
if n mod m = 0
pro *= m
.
.
if n = pro
write n & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
1 6 8 10 14 15 21 22 26 27 33 34 35 38 39 46 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95 106 111 115 118 119 122 123 125 129 133 134 141 142 143 145 146 155 158 159 161 166 177 178 183 185 187 194 201 202 203 205 206 209 213 214 215 217 218 219 221 226 235 237 247 249 253 254 259 262 265 267 274 278 287 291 295 298 299 301 302 303 305 309 314 319 321 323 326 327 329 334 335 339 341 343 346 355 358 362 365 371 377 381 382 386 391 393 394 395 398 403 407 411 413 415 417 422 427 437 445 446 447 451 453 454 458 466 469 471 473 478 481 482 485 489 493 497
</pre>
 
Line 222 ⟶ 399:
=={{header|Go}}==
{{trans|C}}
{{libheader|Go-rcu}}
Run time around 9.2 seconds.
<syntaxhighlight lang="go">package main
 
Line 228 ⟶ 407:
"rcu"
)
 
// library method customized for this task
func Divisors(n int) []int {
var divisors []int
i := 1
k := 1
if n%2 == 1 {
k = 2
}
for ; i*i <= n; i += k {
if i > 1 && n%i == 0 { // exclude 1 and n
divisors = append(divisors, i)
if len(divisors) > 2 { // not eligible if has > 2 divisors
break
}
j := n / i
if j != i {
divisors = append(divisors, j)
}
}
}
return divisors
}
 
func main() {
Line 240 ⟶ 442:
for i := 0; ; i++ {
if i != 1 {
divs = rcu.ProperDivisorsDivisors(i)
} else {
divs = []int{1, 1}
}
length :=if len(divs) == 2 && divs[0]*divs[1] == i {
if length > 1 {count++
prodif :=i 1< 500 {
for d := 1; dfmt.Printf("%3d < length;", d++ {i)
prodif count%10 *== 0 divs[d]{
} fmt.Println()
if prod == i {
count++
if i < 500 {
fmt.Printf("%3d ", i)
if count%10 == 0 {
fmt.Println()
}
}
}
Line 279 ⟶ 474:
scount := rcu.Commatize(count)
st := rcu.Commatize(t)
fmt.Printf("Counts under %7s9s: MPNs = %7s Semi-primes = %7s\n", slimit, scount, st)
if limit == 5000005000000 {
break
}
Line 418 ⟶ 613:
 
Counts under 500000: MPNs = 108222 Semi-primes = 108326
</pre>
 
=={{header|Nim}}==
In our solution, 1 is not considered to be a multiplicatively perfect number.
 
Using 32 bits integers rather than 64 bits integers improves considerably the performance. With a limit set to 5_000_000 and 64 bits integers, the program runs in around 10 seconds. This time drops to 3 seconds if 32 bits integers are used.
<syntaxhighlight lang="Nim">import std/strformat
 
func isMPN(n: int32): bool =
## Return true if "n" is a multiplicatively perfect number.
## We consider than 1 is not an MPN.
var first, second = 0i32 # First and second proper divisors.
let delta = 1 + (n and 1)
var d = delta + 1
while d * d <= n:
if n mod d == 0:
if second != 0: return false # More than two proper divisors.
first = d
let q = n div d
if q != d: second = q
inc d, delta
result = first * second == n
 
### Task ###
var count = 0
for n in 1i32..499i32:
if n.isMPN:
inc count
stdout.write &"{n:3}"
stdout.write if count mod 10 == 0: '\n' else: ' '
echo '\n'
 
### Stretch task ###
 
func isPrime(n: int32): bool =
## Return true if "n" is prime.
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
var k = 5
var delta = 2
while k * k <= n:
if n mod k == 0: return false
inc k, delta
delta = 6 - delta
result = true
 
var mpnCount = 0
var limit = 500i32
var ns, nc = 3i32
var squares, cubes = 1i32
var n = 1i32
while true:
inc n
if n == limit:
while ns * ns < limit:
if ns.isPrime: inc squares
inc ns, 2
while nc * nc * nc < limit:
if nc.isPrime: inc cubes
inc nc, 2
echo &"Under {limit} there are {mpnCount} MPNs and {mpnCount - cubes + squares} semi-primes."
if limit == 500_000: break
limit *= 10
if n.isMPN: inc mpnCount
</syntaxhighlight>
 
{{out]]
<pre> 6 8 10 14 15 21 22 26 27 33
34 35 38 39 46 51 55 57 58 62
65 69 74 77 82 85 86 87 91 93
94 95 106 111 115 118 119 122 123 125
129 133 134 141 142 143 145 146 155 158
159 161 166 177 178 183 185 187 194 201
202 203 205 206 209 213 214 215 217 218
219 221 226 235 237 247 249 253 254 259
262 265 267 274 278 287 291 295 298 299
301 302 303 305 309 314 319 321 323 326
327 329 334 335 339 341 343 346 355 358
362 365 371 377 381 382 386 391 393 394
395 398 403 407 411 413 415 417 422 427
437 445 446 447 451 453 454 458 466 469
471 473 478 481 482 485 489 493 497
 
Under 500 there are 149 MPNs and 153 semi-primes.
Under 5000 there are 1353 MPNs and 1365 semi-primes.
Under 50000 there are 12073 MPNs and 12110 semi-primes.
Under 500000 there are 108222 MPNs and 108326 semi-primes.
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>
use v5.36;
use enum <false true>;
use ntheory <is_prime nth_prime is_semiprime gcd>;
 
sub comma { reverse ((reverse shift) =~ s/.{3}\K/,/gr) =~ s/^,//r }
sub table (@V) { my $t = 10 * (my $w = 5); ( sprintf( ('%'.$w.'d')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }
 
sub find_factor ($n, $constant = 1) { # NB: required that n > 1
my($x, $rho, $factor) = (2, 1, 1);
while ($factor == 1) {
$rho *= 2;
my $fixed = $x;
for (0..$rho) {
$x = ( $x * $x + $constant ) % $n;
$factor = gcd(($x-$fixed), $n);
last if 1 < $factor;
}
}
$factor = find_factor($n, $constant+1) if $n == $factor;
$factor
}
 
# Call with range 1..$limit
sub is_mpn($n) {
state $cube = 1; $cube = 1 if $n == 1; # set and reset
$n == 1 ? return true : is_prime($n) ? return false : ();
++$cube, return true if $n == nth_prime($cube)**3;
my $factor = find_factor($n);
my $div = int $n/$factor;
return true if is_prime $factor and is_prime $div and $div != $factor;
false
}
 
say "Multiplicatively perfect numbers less than 500:\n" . table grep is_mpn($_), 1..499;
 
say 'There are:';
for my $limit (5e2, 5e3, 5e4, 5e5, 5e6) {
my($m,$s) = (0,0);
is_mpn $_ and $m++ for 1..$limit-1;
is_semiprime $_ and $s++ for 1..$limit-1;
printf "%8s MPNs less than %8s, %8s semiprimes\n", comma($m), $limit, comma $s
}
</syntaxhighlight>
{{out}}
<pre>
Multiplicatively perfect numbers less than 500:
1 6 8 10 14 15 21 22 26 27
33 34 35 38 39 46 51 55 57 58
62 65 69 74 77 82 85 86 87 91
93 94 95 106 111 115 118 119 122 123
125 129 133 134 141 142 143 145 146 155
158 159 161 166 177 178 183 185 187 194
201 202 203 205 206 209 213 214 215 217
218 219 221 226 235 237 247 249 253 254
259 262 265 267 274 278 287 291 295 298
299 301 302 303 305 309 314 319 321 323
326 327 329 334 335 339 341 343 346 355
358 362 365 371 377 381 382 386 391 393
394 395 398 403 407 411 413 415 417 422
427 437 445 446 447 451 453 454 458 466
469 471 473 478 481 482 485 489 493 497
 
There are:
150 MPNs less than 500, 153 semiprimes
1,354 MPNs less than 5000, 1,365 semiprimes
12,074 MPNs less than 50000, 12,110 semiprimes
108,223 MPNs less than 500000, 108,326 semiprimes
978,983 MPNs less than 5000000, 979,274 semi primes
</pre>
 
Line 789 ⟶ 1,146:
497 = 7 x 71
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ { 1 }
2 ROT '''FOR''' j
'''IF''' j SQ j DIVIS ΠLIST == '''THEN''' j + '''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
≪ (1,0)
2 ROT '''FOR''' j
j DIVIS
'''IF CASE'''
DUP SIZE DUP 4 > OVER " < '''THEN''' NOT '''END'''
3 == '''THEN''' 1 '''END'''
DUP 2 GET OVER 3 GET GCD 1 == '''END'''
'''THEN''' SWAP (0,1) + SWAP '''END'''
'''IF''' ΠLIST j SQ == '''THEN''' 1 + '''END'''
'''NEXT'''
≫ '<span style="color:blue">STRETCH</span>' STO
 
500 <span style="color:blue">TASK</span>
500 <span style="color:blue">STRETCH</span>
{{out}}
<pre>
2: { 1 6 8 10 14 15 21 22 26 27 33 34 35 38 39 46 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95 106 111 115 118 119 122 123 125 129 133 134 141 142 143 145 146 155 158 159 161 166 177 178 183 185 187 194 201 202 203 205 206 209 213 214 215 217 218 219 221 226 235 237 247 249 253 254 259 262 265 267 274 278 287 291 295 298 299 301 302 303 305 309 314 319 321 323 326 327 329 334 335 339 341 343 346 355 358 362 365 371 377 381 382 386 391 393 394 395 398 403 407 411 413 415 417 422 427 437 445 446 447 451 453 454 458 466 469 471 473 478 481 482 485 489 493 497 }
1: (150,153)
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn divisors( num : u128 ) -> Vec<u128> {
(1..= num).filter( | &d | num % d == 0 ).collect( )
}
 
fn main() {
println!("{:?}" , (1..= 500).filter( | &d | {
let divis : Vec<u128> = divisors( d ) ;
let prod : u128 = divis.iter( ).product( ) ;
prod == d.checked_pow( 2 ).unwrap( )
}).collect::<Vec<u128>>( ) ) ;
}</syntaxhighlight>
{{out}}
<pre>
[1, 6, 8, 10, 14, 15, 21, 22, 26, 27, 33, 34, 35, 38, 39, 46, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95, 106, 111, 115, 118, 119, 122, 123, 125, 129, 133, 134, 141, 142, 143, 145, 146, 155, 158, 159, 161, 166, 177, 178, 183, 185, 187, 194, 201, 202, 203, 205, 206, 209, 213, 214, 215, 217, 218, 219, 221, 226, 235, 237, 247, 249, 253, 254, 259, 262, 265, 267, 274, 278, 287, 291, 295, 298, 299, 301, 302, 303, 305, 309, 314, 319, 321, 323, 326, 327, 329, 334, 335, 339, 341, 343, 346, 355, 358, 362, 365, 371, 377, 381, 382, 386, 391, 393, 394, 395, 398, 403, 407, 411, 413, 415, 417, 422, 427, 437, 445, 446, 447, 451, 453, 454, 458, 466, 469, 471, 473, 478, 481, 482, 485, 489, 493, 497]
</pre>
 
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func multiplicatively_perfect_numbers(N) {
[1] + semiprimes(N).grep{|n| isqrt(n**tau(n)) == n.sqr } + N.icbrt.primes.map { .cube } -> sort
}
 
say ("Terms <= 500: ", multiplicatively_perfect_numbers(500).join(' '), "\n")
 
for n in (500, 5_000, 50_000, 500_000) {
var M = multiplicatively_perfect_numbers(n)
say "There are #{M.len} MPNs and #{semiprime_count(n)} semiprimes <= #{n.commify}."
}</syntaxhighlight>
{{out}}
<pre>
Terms <= 500: 1 6 8 10 14 15 21 22 26 27 33 34 35 38 39 46 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95 106 111 115 118 119 122 123 125 129 133 134 141 142 143 145 146 155 158 159 161 166 177 178 183 185 187 194 201 202 203 205 206 209 213 214 215 217 218 219 221 226 235 237 247 249 253 254 259 262 265 267 274 278 287 291 295 298 299 301 302 303 305 309 314 319 321 323 326 327 329 334 335 339 341 343 346 355 358 362 365 371 377 381 382 386 391 393 394 395 398 403 407 411 413 415 417 422 427 437 445 446 447 451 453 454 458 466 469 471 473 478 481 482 485 489 493 497
 
There are 150 MPNs and 153 semiprimes <= 500.
There are 1354 MPNs and 1365 semiprimes <= 5,000.
There are 12074 MPNs and 12110 semiprimes <= 50,000.
There are 108223 MPNs and 108326 semiprimes <= 500,000.
</pre>
 
Line 795 ⟶ 1,219:
{{libheader|Wren-fmt}}
This includes '1' as an MPN. Not very quick at around 112 seconds but reasonable for Wren.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
2,022

edits