Sequence: nth number with exactly n divisors: Difference between revisions

m
m (→‎{{header|REXX}}: moved a function.)
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 9 users not shown)
Line 14:
:*[[Sequence: smallest number greater than previous term with exactly n divisors]]
:*[[Sequence: smallest number with exactly n divisors]]
<br/><br/>
 
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT (with default precision) to handle the large numbers.<br/>
Builds a table of proper divisor counts for testing non-prime n (As with other samples uses the fact that for prime n, the required number is the nth prime to the power n - 1.<br/>
Using a table is practical for smallish n, (a table of 500 000 elements is used here, big enough for up to n = 24. For n = 25, a table of over 52 000 000 would be required.<br/>
For non-prime n, the divisors are also shown.
<syntaxhighlight lang="algol68">
BEGIN
INT max number = 500 000; # maximum number we will count the divisors of #
# form a table of proper divisor counts #
[ 1 : max number ]INT pdc; FOR i TO UPB pdc DO pdc[ i ] := 1 OD;
pdc[ 1 ] := 0;
FOR i FROM 2 TO UPB pdc DO
FOR j FROM i + i BY i TO UPB pdc DO pdc[ j ] +:= 1 OD
OD;
# find the first few primes #
[ 1 : 30 ]INT prime;
INT p count := 0;
FOR i WHILE p count < UPB prime DO
IF pdc[ i ] = 1 THEN prime[ p count +:= 1 ] := i FI
OD;
# show the nth number with n divisors #
INT w = -43; # width to print the numbers (negative means no leading +) #
print( ( " 1: ", whole( 1, w ), " | 1", newline ) );
FOR i FROM 2 TO 23 DO
print( ( whole( i, -2 ), ": " ) );
IF pdc( i ) = 1 THEN
print( ( whole( LENG LENG prime[ i ] ^ ( i - 1 ), w ), newline ) )
ELSE
INT c := 0;
FOR j TO UPB pdc WHILE c < i DO
IF pdc[ j ] = i - 1 THEN
c +:= 1;
IF c = i THEN
print( ( whole( j, w ), " | 1" ) );
FOR d FROM 2 TO j OVER 2 DO
IF j MOD d = 0 THEN print( ( " ", whole( d, 0 ) ) ) FI
OD;
print( ( " ", whole( j, 0 ), newline ) )
FI
FI
OD
FI
OD
 
END
</syntaxhighlight>
{{out}}
<pre style="font-size: 12px">
1: 1 | 1
2: 3
3: 25
4: 14 | 1 2 7 14
5: 14641
6: 44 | 1 2 4 11 22 44
7: 24137569
8: 70 | 1 2 5 7 10 14 35 70
9: 1089 | 1 3 9 11 33 99 121 363 1089
10: 405 | 1 3 5 9 15 27 45 81 135 405
11: 819628286980801
12: 160 | 1 2 4 5 8 10 16 20 32 40 80 160
13: 22563490300366186081
14: 2752 | 1 2 4 8 16 32 43 64 86 172 344 688 1376 2752
15: 9801 | 1 3 9 11 27 33 81 99 121 297 363 891 1089 3267 9801
16: 462 | 1 2 3 6 7 11 14 21 22 33 42 66 77 154 231 462
17: 21559177407076402401757871041
18: 1044 | 1 2 3 4 6 9 12 18 29 36 58 87 116 174 261 348 522 1044
19: 740195513856780056217081017732809
20: 1520 | 1 2 4 5 8 10 16 19 20 38 40 76 80 95 152 190 304 380 760 1520
21: 141376 | 1 2 4 8 16 32 47 64 94 188 376 752 1504 2209 3008 4418 8836 17672 35344 70688 141376
22: 84992 | 1 2 4 8 16 32 64 83 128 166 256 332 512 664 1024 1328 2656 5312 10624 21248 42496 84992
23: 1658509762573818415340429240403156732495289</pre>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
 
#define LIMIT 15
int smallPrimes[LIMIT];
 
static void sieve() {
int i = 2, j;
int p = 5;
 
smallPrimes[0] = 2;
smallPrimes[1] = 3;
 
while (i < LIMIT) {
for (j = 0; j < i; j++) {
if (smallPrimes[j] * smallPrimes[j] <= p) {
if (p % smallPrimes[j] == 0) {
p += 2;
break;
}
} else {
smallPrimes[i++] = p;
p += 2;
break;
}
}
}
}
 
static bool is_prime(uint64_t n) {
uint64_t i;
 
for (i = 0; i < LIMIT; i++) {
if (n % smallPrimes[i] == 0) {
return n == smallPrimes[i];
}
}
 
i = smallPrimes[LIMIT - 1] + 2;
for (; i * i <= n; i += 2) {
if (n % i == 0) {
return false;
}
}
 
return true;
}
 
static uint64_t divisor_count(uint64_t n) {
uint64_t count = 1;
uint64_t d;
 
while (n % 2 == 0) {
n /= 2;
count++;
}
 
for (d = 3; d * d <= n; d += 2) {
uint64_t q = n / d;
uint64_t r = n % d;
uint64_t dc = 0;
while (r == 0) {
dc += count;
n = q;
q = n / d;
r = n % d;
}
count += dc;
}
 
if (n != 1) {
return count *= 2;
}
return count;
}
 
static uint64_t OEISA073916(size_t n) {
uint64_t count = 0;
uint64_t result = 0;
size_t i;
 
if (is_prime(n)) {
return (uint64_t)pow(smallPrimes[n - 1], n - 1);
}
 
for (i = 1; count < n; i++) {
if (n % 2 == 1) {
// The solution for an odd (non-prime) term is always a square number
uint64_t root = (uint64_t)sqrt(i);
if (root * root != i) {
continue;
}
}
if (divisor_count(i) == n) {
count++;
result = i;
}
}
 
return result;
}
 
int main() {
size_t n;
 
sieve();
 
for (n = 1; n <= LIMIT; n++) {
if (n == 13) {
printf("A073916(%lu) = One more bit needed to represent result.\n", n);
} else {
printf("A073916(%lu) = %llu\n", n, OEISA073916(n));
}
}
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>A073916(1) = 1
A073916(2) = 3
A073916(3) = 25
A073916(4) = 14
A073916(5) = 14641
A073916(6) = 44
A073916(7) = 24137569
A073916(8) = 70
A073916(9) = 1089
A073916(10) = 405
A073916(11) = 819628286980801
A073916(12) = 160
A073916(13) = One more bit needed to represent result.
A073916(14) = 2752
A073916(15) = 9801</pre>
 
=={{header|C++}}==
{{trans|Java}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
std::vector<int> smallPrimes;
 
bool is_prime(size_t test) {
if (test < 2) {
return false;
}
if (test % 2 == 0) {
return test == 2;
}
for (size_t d = 3; d * d <= test; d += 2) {
if (test % d == 0) {
return false;
}
}
return true;
}
 
void init_small_primes(size_t numPrimes) {
smallPrimes.push_back(2);
 
int count = 0;
for (size_t n = 3; count < numPrimes; n += 2) {
if (is_prime(n)) {
smallPrimes.push_back(n);
count++;
}
}
}
 
size_t divisor_count(size_t n) {
size_t count = 1;
while (n % 2 == 0) {
n /= 2;
count++;
}
for (size_t d = 3; d * d <= n; d += 2) {
size_t q = n / d;
size_t r = n % d;
size_t dc = 0;
while (r == 0) {
dc += count;
n = q;
q = n / d;
r = n % d;
}
count += dc;
}
if (n != 1) {
count *= 2;
}
return count;
}
 
uint64_t OEISA073916(size_t n) {
if (is_prime(n)) {
return (uint64_t) pow(smallPrimes[n - 1], n - 1);
}
 
size_t count = 0;
uint64_t result = 0;
for (size_t i = 1; count < n; i++) {
if (n % 2 == 1) {
// The solution for an odd (non-prime) term is always a square number
size_t root = (size_t) sqrt(i);
if (root * root != i) {
continue;
}
}
if (divisor_count(i) == n) {
count++;
result = i;
}
}
return result;
}
 
int main() {
const int MAX = 15;
init_small_primes(MAX);
for (size_t n = 1; n <= MAX; n++) {
if (n == 13) {
std::cout << "A073916(" << n << ") = One more bit needed to represent result.\n";
} else {
std::cout << "A073916(" << n << ") = " << OEISA073916(n) << '\n';
}
}
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>A073916(1) = 1
A073916(2) = 3
A073916(3) = 25
A073916(4) = 14
A073916(5) = 14641
A073916(6) = 44
A073916(7) = 24137569
A073916(8) = 70
A073916(9) = 1089
A073916(10) = 405
A073916(11) = 819628286980801
A073916(12) = 160
A073916(13) = One more bit needed to represent result.
A073916(14) = 2752
A073916(15) = 9801</pre>
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang="d">import std.bigint;
import std.math;
import std.stdio;
 
bool isPrime(long test) {
if (test == 2) {
return true;
}
if (test % 2 == 0) {
return false;
}
for (long d = 3 ; d * d <= test; d += 2) {
if (test % d == 0) {
return false;
}
}
return true;
}
 
int[] calcSmallPrimes(int numPrimes) {
int[] smallPrimes;
smallPrimes ~= 2;
 
int count = 0;
int n = 3;
while (count < numPrimes) {
if (isPrime(n)) {
smallPrimes ~= n;
count++;
}
n += 2;
}
 
return smallPrimes;
}
 
immutable MAX = 45;
immutable smallPrimes = calcSmallPrimes(MAX);
 
int getDivisorCount(long n) {
int count = 1;
while (n % 2 == 0) {
n /= 2;
count += 1;
}
for (long d = 3; d * d <= n; d += 2) {
long q = n / d;
long r = n % d;
int dc = 0;
while (r == 0) {
dc += count;
n = q;
q = n / d;
r = n % d;
}
count += dc;
}
if (n != 1) {
count *= 2;
}
return count;
}
 
BigInt OEISA073916(int n) {
if (isPrime(n) ) {
return BigInt(smallPrimes[n-1]) ^^ (n - 1);
}
int count = 0;
int result = 0;
for (int i = 1; count < n; i++) {
if (n % 2 == 1) {
// The solution for an odd (non-prime) term is always a square number
int root = cast(int) sqrt(cast(real) i);
if (root * root != i) {
continue;
}
}
if (getDivisorCount(i) == n) {
count++;
result = i;
}
}
return BigInt(result);
}
 
void main() {
foreach (n; 1 .. MAX + 1) {
writeln("A073916(", n, ") = ", OEISA073916(n));
}
}</syntaxhighlight>
{{out}}
<pre>A073916(1) = 1
A073916(2) = 3
A073916(3) = 25
A073916(4) = 14
A073916(5) = 14641
A073916(6) = 44
A073916(7) = 24137569
A073916(8) = 70
A073916(9) = 1089
A073916(10) = 405
A073916(11) = 819628286980801
A073916(12) = 160
A073916(13) = 22563490300366186081
A073916(14) = 2752
A073916(15) = 9801
A073916(16) = 462
A073916(17) = 21559177407076402401757871041
A073916(18) = 1044
A073916(19) = 740195513856780056217081017732809
A073916(20) = 1520
A073916(21) = 141376
A073916(22) = 84992
A073916(23) = 1658509762573818415340429240403156732495289
A073916(24) = 1170
A073916(25) = 52200625
A073916(26) = 421888
A073916(27) = 52900
A073916(28) = 9152
A073916(29) = 1116713952456127112240969687448211536647543601817400964721
A073916(30) = 6768
A073916(31) = 1300503809464370725741704158412711229899345159119325157292552449
A073916(32) = 3990
A073916(33) = 12166144
A073916(34) = 9764864
A073916(35) = 446265625
A073916(36) = 5472
A073916(37) = 11282036144040442334289838466416927162302790252609308623697164994458730076798801
A073916(38) = 43778048
A073916(39) = 90935296
A073916(40) = 10416
A073916(41) = 1300532588674810624476094551095787816112173600565095470117230812218524514342511947837104801
A073916(42) = 46400
A073916(43) = 635918448514386699807643535977466343285944704172890141356181792680152445568879925105775366910081
A073916(44) = 240640
A073916(45) = 327184</pre>
 
=={{header|Factor}}==
This makes use of most of the optimizations discussed in the Go example.
<langsyntaxhighlight lang="factor">USING: combinators formatting fry kernel lists lists.lazy
lists.lazy.examples literals math math.functions math.primes
math.primes.factors math.ranges sequences ;
Line 48 ⟶ 511:
: main ( -- ) 45 [1,b] [ dup fn "%2d : %d\n" printf ] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 97 ⟶ 560:
45 : 327184
</pre>
 
 
=={{header|FreeBASIC}}==
But it takes an "eternity" to resolve.
<syntaxhighlight lang="freebasic">Dim As Integer n, num, pnum
Dim As Ulongint m, p
Dim As Ulongint limit = 18446744073709551615
 
Print "The first 15 terms of OEIS:A073916 are:"
For n = 1 To 15
num = 0
For m = 1 To limit
pnum = 0
For p = 1 To limit
If (m Mod p = 0) Then pnum += 1
Next p
If pnum = n Then num += 1
If num = n Then
Print Using "## : &"; n; m
Exit For
End If
Next m
Next n
Sleep</syntaxhighlight>
 
 
=={{header|Go}}==
Line 102 ⟶ 590:
 
The remaining terms (up to the 33rd) are not particularly large and so are calculated by brute force.
<langsyntaxhighlight lang="go">package main
 
import (
Line 184 ⟶ 672:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 232 ⟶ 720:
3. While searching for the nth number with exactly n divisors, where feasible a record is kept of any numbers found to have exactly k divisors (k > n) so that the search for these numbers can start from a higher base.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 352 ⟶ 840:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 404 ⟶ 892:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (guard)
import Math.NumberTheory.ArithmeticFunctions (divisorCount)
import Math.NumberTheory.Primes (Prime, unPrime)
Line 431 ⟶ 919:
 
main :: IO ()
main = mapM_ print nths</langsyntaxhighlight>
{{out}}
<pre>(1,1)
Line 468 ⟶ 956:
(34,9764864)
(35,446265625)</pre>
 
=={{header|J}}==
 
Implementation:
 
<syntaxhighlight lang="j">
A073916=: {{
if.1 p: y do. (p:^x:)y-1 return.
elseif.1|y do.
f= *:
else.
f=. ]
end. r=.i.0
off=. 1
while. y>#r do.
r=. r,f off+I.y=*/|:1+_ q:f off+i.y
off=. off+y
end.
(y-1){r
}}"0
</syntaxhighlight>
 
Task example:
 
<syntaxhighlight lang="j">
(,. A073916) 1+i.20
1 1
2 3
3 25
4 14
5 14641
6 44
7 24137569
8 70
9 1089
10 405
11 819628286980801
12 160
13 22563490300366186081
14 2752
15 9801
16 462
17 21559177407076402401757871041
18 1044
19 740195513856780056217081017732809
20 1520
</syntaxhighlight>
 
=={{header|Java}}==
Line 473 ⟶ 1,008:
Replace translation with Java native implementation.
 
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.ArrayList;
Line 562 ⟶ 1,097:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 611 ⟶ 1,146:
A073916(44) = 240640
A073916(45) = 327184
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''<br>
'''Works with gojq, the Go implementation of jq'''
 
See [[Erd%C5%91s-primes#jq]] for a suitable implementation of
`is_prime`.
 
The precision of the integer arithmetic of the C implementation of jq
is only precise enough
for computing the n-th value up to and including [16,462].
Accordingly gojq was used to produce the output shown below.
 
'''Preliminaries'''
<syntaxhighlight lang="jq">def count(stream): reduce stream as $i (0; .+1);
 
# To maintain precision:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
def primes: 2, (range(3; infinite; 2) | select(is_prime));
 
# divisors as an unsorted stream
def divisors:
if . == 1 then 1
else . as $n
| label $out
| range(1; $n) as $i
| ($i * $i) as $i2
| if $i2 > $n then break $out
else if $i2 == $n
then $i
elif ($n % $i) == 0
then $i, ($n/$i)
else empty
end
end
end;
</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="jq"># Emit [n, nth_with_n_divisors] for n in range(1; .+1)
def nth_with_n_divisors:
| [limit( .; primes)] as $primes
| range( 1; 1 + .) as $i
| if $i | is_prime
then [$i, ($primes[$i-1]|power($i-1))]
else {count: 0, j: 1}
| until(.count == $i ;
.cont = false
| if ($i % 2) == 1 then (.j|sqrt|floor) as $sq
| if ($sq * $sq) != .j then .j += 1 | .cont = true else . end
else .
end
| if .cont == false
then if (.j | count(divisors)) == $i
then .count += 1
else .
end
| if .count != $i then .j += 1 else . end
else .
end )
 
| [ $i, .j]
end;
 
"The first 33 terms in the sequence are:",
(33 | nth_with_n_divisors)</syntaxhighlight>
{{out}}
<pre>
The first 33 terms in the sequence are:
[1,1]
[2,3]
[3,25]
[4,14]
[5,14641]
[6,44]
[7,24137569]
[8,70]
[9,1089]
[10,405]
[11,819628286980801]
[12,160]
[13,22563490300366186081]
[14,2752]
[15,9801]
[16,462]
[17,21559177407076402401757871041]
[18,1044]
[19,740195513856780056217081017732809]
[20,1520]
[21,141376]
[22,84992]
[23,1658509762573818415340429240403156732495289]
[24,1170]
[25,52200625]
[26,421888]
[27,52900]
[28,9152]
[29,1116713952456127112240969687448211536647543601817400964721]
[30,6768]
[31,1300503809464370725741704158412711229899345159119325157292552449]
[32,3990]
[33,12166144]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function countdivisors(n)
Line 643 ⟶ 1,281:
 
nthwithndivisors(35)
</langsyntaxhighlight>{{out}}
<pre>
1 : 1
Line 684 ⟶ 1,322:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.3.21
 
import java.math.BigInteger
Line 762 ⟶ 1,400:
}
}
}</langsyntaxhighlight>
 
{{output}}
Line 801 ⟶ 1,439:
33 : 12166144
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">d = Table[
Length[Divisors[n]], {n,
200000}]; t = {}; n = 0; ok = True; While[ok, n++;
If[PrimeQ[n], AppendTo[t, Prime[n]^(n - 1)],
c = Flatten[Position[d, n, 1, n]];
If[Length[c] >= n, AppendTo[t, c[[n]]], ok = False]]];
t</syntaxhighlight>
{{out}}
<pre>{1,3,25,14,14641,44,24137569,70,1089,405,819628286980801,160,22563490300366186081,2752,9801,462,21559177407076402401757871041,1044,740195513856780056217081017732809,1520,141376,84992,1658509762573818415340429240403156732495289,1170}</pre>
 
=={{header|Nim}}==
{{trans|Go}}
{{libheader|bignum}}
This is a translation of the fast Go version. It runs in about 23s on our laptop.
<syntaxhighlight lang="nim">import math, strformat
import bignum
 
type Record = tuple[num, count: Natural]
 
template isOdd(n: Natural): bool =
(n and 1) != 0
 
func isPrime(n: int): bool =
let bi = newInt(n)
result = bi.probablyPrime(25) != 0
 
proc findPrimes(limit: Natural): seq[int] {.compileTime.} =
result = @[2]
var isComposite = newSeq[bool](limit + 1)
var p = 3
while true:
let p2 = p * p
if p2 > limit: break
for i in countup(p2, limit, 2 * p):
isComposite[i] = true
while true:
inc p, 2
if not isComposite[p]: break
for n in countup(3, limit, 2):
if not isComposite[n]:
result.add n
 
const Primes = findPrimes(22_000)
 
proc countDivisors(n: Natural): int =
result = 1
var n = n
for i, p in Primes:
if p * p > n: break
if n mod p != 0: continue
n = n div p
var count = 1
while n mod p == 0:
n = n div p
inc count
result *= count + 1
if n == 1: return
if n != 1: result *= 2
 
const Max = 45
var records: array[0..Max, Record]
echo &"The first {Max} terms in the sequence are:"
 
for n in 1..Max:
 
if n.isPrime:
var z = newInt(Primes[n - 1])
z = pow(z, culong(n - 1))
echo &"{n:2}: {z}"
 
else:
var count = records[n].count
if count == n:
echo &"{n:2}: {records[n].num}"
continue
let odd = n.isOdd
let d = if odd or n == 2 or n == 10: 1 else: 2
var k = records[n].num
while true:
inc k, d
if odd:
let sq = sqrt(k.toFloat).int
if sq * sq != k: continue
let cd = k.countDivisors()
if cd == n:
inc count
if count == n:
echo &"{n:2}: {k}"
break
elif cd in (n + 1)..Max and records[cd].count < cd and
k > records[cd].num and (d == 1 or d == 2 and not cd.isOdd):
records[cd].num = k
inc records[cd].count</syntaxhighlight>
 
{{out}}
<pre>The first 45 terms in the sequence are:
1: 1
2: 3
3: 25
4: 14
5: 14641
6: 44
7: 24137569
8: 70
9: 1089
10: 405
11: 819628286980801
12: 160
13: 22563490300366186081
14: 2752
15: 9801
16: 462
17: 21559177407076402401757871041
18: 1044
19: 740195513856780056217081017732809
20: 1520
21: 141376
22: 84992
23: 1658509762573818415340429240403156732495289
24: 1170
25: 52200625
26: 421888
27: 52900
28: 9152
29: 1116713952456127112240969687448211536647543601817400964721
30: 6768
31: 1300503809464370725741704158412711229899345159119325157292552449
32: 3990
33: 12166144
34: 9764864
35: 446265625
36: 5472
37: 11282036144040442334289838466416927162302790252609308623697164994458730076798801
38: 43778048
39: 90935296
40: 10416
41: 1300532588674810624476094551095787816112173600565095470117230812218524514342511947837104801
42: 46400
43: 635918448514386699807643535977466343285944704172890141356181792680152445568879925105775366910081
44: 240640
45: 327184</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use bigint;
Line 825 ⟶ 1,606:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 terms of OEIS:A073916
Line 835 ⟶ 1,616:
Certainly not the fastest way to do it, hence the relatively small limit of 24, which takes less than 0.4s,<br>
whereas a limit of 25 would need to invoke factors() 52 million times which would no doubt take a fair while.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant LIMIT = 24
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
include mpfr.e
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">24</span>
mpz z = mpz_init()
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
sequence fn = 1&repeat(0,LIMIT-1)
integer k = 1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">LIMIT</span><span style="color: #0000FF;">)</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
printf(1,"The first %d terms in the sequence are:\n",LIMIT)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
for i=1 to LIMIT do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d terms in the sequence are:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">LIMIT</span><span style="color: #0000FF;">)</span>
if is_prime(i) then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">LIMIT</span> <span style="color: #008080;">do</span>
mpz_ui_pow_ui(z,get_prime(i),i-1)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
printf(1,"%2d : %s\n",{i,mpz_get_str(z)})
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
else
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)})</span>
while fn[i]<i do
<span style="color: #008080;">else</span>
k += 1
<span style="color: #008080;">while</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
integer l = length(factors(k,1))
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if l<=LIMIT and fn[l]<l then
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
fn[l] = iff(fn[l]+1<l?fn[l]+1:k)
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">LIMIT</span> <span style="color: #008080;">and</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">l</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;"><</span><span style="color: #000000;">l</span><span style="color: #0000FF;">?</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"%2d : %d\n",{i,fn[i]})
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d : %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 888 ⟶ 1,672:
===cheating slightly===
No real patterns that I could see here, but you can still identify and single out the troublemakers (of which there are about 30).
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom t0 = time()
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
constant LIMIT = 100
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
include mpfr.e
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span>
include primes.e
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
mpz z = mpz_init(),
<span style="color: #008080;">include</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
p = mpz_init()
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(),</span>
string mz
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
sequence fn = 1&repeat(0,LIMIT-1), dx
<span style="color: #004080;">string</span> <span style="color: #000000;">mz</span>
integer k = 1, idx, p1, p2
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">LIMIT</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">;</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
printf(1,"The first %d terms in the sequence are:\n",LIMIT)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p2</span>
for i=1 to LIMIT do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d terms in the sequence are:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">LIMIT</span><span style="color: #0000FF;">)</span>
if is_prime(i) or i=1 then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">LIMIT</span> <span style="color: #008080;">do</span>
mpz_ui_pow_ui(z,get_prime(i),i-1)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
mz = mpz_get_str(z)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
else
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
sequence f = prime_factors(i,1)
<span style="color: #008080;">else</span>
if length(f)=2 and f[1]=2 and f[2]>7 then
<span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
mz = sprintf("%d",power(2,f[2]-1)*get_prime(i+1))
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]></span><span style="color: #000000;">7</span> <span style="color: #008080;">then</span>
elsif length(f)=2 and f[1]>2 then
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
if f[1]=f[2] then
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]></span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
mz = sprintf("%d",power(f[1]*get_prime(f[1]+2),f[1]-1))
<span style="color: #008080;">if</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
else -- deal with some tardy ones...
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]*</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
dx = {15,21,33,35,39,51,55,57,65,69,77,85,87,91,93,95}; idx = find(i,dx)
<span style="color: #008080;">else</span> <span style="color: #000080;font-style:italic;">-- deal with some tardy ones...</span>
p1 = { 3, 2, 2, 5, 2, 2, 2, 2, 2, 2, 7, 2, 2, 7, 2, 2}[idx]
<span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">33</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">,</span><span style="color: #000000;">39</span><span style="color: #0000FF;">,</span><span style="color: #000000;">51</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">,</span><span style="color: #000000;">57</span><span style="color: #0000FF;">,</span><span style="color: #000000;">65</span><span style="color: #0000FF;">,</span><span style="color: #000000;">69</span><span style="color: #0000FF;">,</span><span style="color: #000000;">77</span><span style="color: #0000FF;">,</span><span style="color: #000000;">85</span><span style="color: #0000FF;">,</span><span style="color: #000000;">87</span><span style="color: #0000FF;">,</span><span style="color: #000000;">91</span><span style="color: #0000FF;">,</span><span style="color: #000000;">93</span><span style="color: #0000FF;">,</span><span style="color: #000000;">95</span><span style="color: #0000FF;">};</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
p2 = { 5,15,29, 6,35,49,34,56,45,69, 7,65,88, 7,94,77}[idx]
<span style="color: #000000;">p1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
mpz_ui_pow_ui(z,p1,f[2]-1)
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">56</span><span style="color: #0000FF;">,</span><span style="color: #000000;">45</span><span style="color: #0000FF;">,</span><span style="color: #000000;">69</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">65</span><span style="color: #0000FF;">,</span><span style="color: #000000;">88</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">94</span><span style="color: #0000FF;">,</span><span style="color: #000000;">77</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
mpz_ui_pow_ui(p,get_prime(p2),f[1]-1)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
mpz_mul(z,z,p)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
mz = mpz_get_str(z)
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
elsif (length(f)=3 and i>50) or (length(f)=4 and (f[1]=3 or f[4]>7)) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if i=99 then -- (oops, messed that one up!)
<span style="color: #008080;">elsif</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">3</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">4</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">3</span> <span style="color: #008080;">or</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]></span><span style="color: #000000;">7</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
mz = sprintf("%d",4*power(3,10)*31*31)
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">99</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (oops, messed that one up!)</span>
elsif i=63 then -- (and another!)
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">31</span><span style="color: #0000FF;">*</span><span style="color: #000000;">31</span><span style="color: #0000FF;">)</span>
mz = sprintf("%d",power(2,8)*power(5,6))
<span style="color: #008080;">elsif</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">63</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (and another!)</span>
else
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">))</span>
dx = {52,66,68,70,75,76,78,92,98,81,88}; idx = find(i,dx)
<span p1 style="color: { 7, 3, 1, 5, 3, 5, 5,13, 3,35,35}[idx]#008080;">else</span>
<span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">52</span><span style="color: #0000FF;">,</span><span style="color: #000000;">66</span><span style="color: #0000FF;">,</span><span style="color: #000000;">68</span><span style="color: #0000FF;">,</span><span style="color: #000000;">70</span><span style="color: #0000FF;">,</span><span style="color: #000000;">75</span><span style="color: #0000FF;">,</span><span style="color: #000000;">76</span><span style="color: #0000FF;">,</span><span style="color: #000000;">78</span><span style="color: #0000FF;">,</span><span style="color: #000000;">92</span><span style="color: #0000FF;">,</span><span style="color: #000000;">98</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">,</span><span style="color: #000000;">88</span><span style="color: #0000FF;">};</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">)</span>
p2 = { 1, 2, 1, 4, 4, 1, 2, 1, 1, 2, 1}[idx]
<span style="color: #000000;">p1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">,</span><span style="color: #000000;">35</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
mpz_ui_pow_ui(z,2,f[$]-1)
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
mpz_ui_pow_ui(p,p1,p2)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">[$]-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
mpz_mul(z,z,p)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span>
p1 = {13,37, 4, 9,34,22,19,12, 4,11,13}[idx]
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
p2 = { 1, 1, 3, 1, 2, 1, 1, 1, 6, 2, 1}[idx]
<span style="color: #000000;">p1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">37</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">22</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
mpz_ui_pow_ui(p,get_prime(p1),p2)
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
mpz_mul(z,z,p)
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">p2</span><span style="color: #0000FF;">)</span>
mz = mpz_get_str(z)
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
else
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
while fn[i]<i do
<span k +style="color: 1#008080;">else</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
integer l = length(factors(k,1))
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if l<=LIMIT and fn[l]<l then
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
fn[l] = iff(fn[l]+1<l?fn[l]+1:k)
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">LIMIT</span> <span style="color: #008080;">and</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">l</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;"><</span><span style="color: #000000;">l</span><span style="color: #0000FF;">?</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
mz = sprintf("%d",fn[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end if
<span style="color: #000000;">mz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"%3d : %s\n",{i,mz})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mz</span><span style="color: #0000FF;">})</span>
printf(1,"completed in %s\n",{elapsed(time()-t0)})</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"completed in %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,059 ⟶ 1,846:
=={{header|Python}}==
This implementation exploits the fact that terms corresponding to a prime value for n are always the nth prime to the (n-1)th power.
<syntaxhighlight lang="python">
<lang Python>
def divisors(n):
divs = [1]
Line 1,123 ⟶ 1,910:
for item in sequence(15):
print(item)
</syntaxhighlight>
</lang>
<b>Output:</b>
<syntaxhighlight lang="python">
<lang Python>
1
3
Line 1,141 ⟶ 1,928:
2752
9801
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,149 ⟶ 1,936:
[https://tio.run/##dVLbTsJAEH3nKw4IpgW6ATQY2QAao4kvmsijEFPoVpr05nZr2hh@yk/wx@p0CwUf3JfuzJwzc/ZMYyH9cVEk6RqO92ltojRUMJaZia8G6EihUhliBM9FxrzEiqUXCK5rPde3CTwEY1RLPqRirh9F0mSBHU9gzbB09m3Kk4a@SBJk6IDSCHIsc0wppsFwOCiYUmU@p1uzCSPvwzGx0/xdg74NorR9L/AU0UYDXmVutKKEUu9SxNS4VoldH0PGuryxx7xW7BXHGSqE2grYUto5w@Lu6YU6xqlC68GTiTrMUkIGCSIXz/ePi8nt4OriejhucY00qH8FM9k2j4U0JqO1rTbbowftcO@BQbcZLmGHDiVrlSa9WNdFFpcQC8M@asE6XplkiMY40YmhpR0evXvA/6ZIsK0iSRWidzq0PPK0VNo1tbH6Vunr/nwfyTWTueX7JyejyhOKTB2WSI1pWey8/mf4v9BerxRZavmLab/VYb1jXhS/ Try it online!]
 
<syntaxhighlight lang="raku" perl6line>sub div-count (\x) {
return 2 if x.is-prime;
+flat (1 .. x.sqrt.floor).map: -> \d {
Line 1,174 ⟶ 1,961:
}
}
};</langsyntaxhighlight>
 
<pre>First 20 terms of OEIS:A073916
Line 1,182 ⟶ 1,969:
Programming note: &nbsp; this REXX version has minor optimization, and all terms of the sequence are determined (found) in order.
===little optimization===
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the Nth number with exactly N divisors. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,241 ⟶ 2,028:
do j=11 by 6 until j*j>#; if # // j==0 | # // (J+2)==0 then return 0
end /*j*/ /* ___ */
return 1 /*Exceeded √ # ? Then # is prime. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input: &nbsp; &nbsp; <tt> 45 </tt>}}
 
Line 1,312 ⟶ 2,099:
This REXX version (unlike the 1<sup>st</sup> version), &nbsp; only goes through the numbers once, instead
of looking for numbers that have specific number of divisors.
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the Nth number with exactly N divisors. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,388 ⟶ 2,175:
if #//(i+2)==0 then return 0
end /*i*/ /* ___ */
return 1 /*Exceeded √ # ? Then # is prime. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,421 ⟶ 2,208:
 
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,443 ⟶ 2,230:
done...
</pre>
 
=={{header|Ruby}}==
{{trans|Java}}
<syntaxhighlight lang="ruby">def isPrime(n)
return false if n < 2
return n == 2 if n % 2 == 0
return n == 3 if n % 3 == 0
 
k = 5
while k * k <= n
return false if n % k == 0
k = k + 2
end
 
return true
end
 
def getSmallPrimes(numPrimes)
smallPrimes = [2]
count = 0
n = 3
while count < numPrimes
if isPrime(n) then
smallPrimes << n
count = count + 1
end
n = n + 2
end
return smallPrimes
end
 
def getDivisorCount(n)
count = 1
while n % 2 == 0
n = (n / 2).floor
count = count + 1
end
 
d = 3
while d * d <= n
q = (n / d).floor
r = n % d
dc = 0
while r == 0
dc = dc + count
n = q
q = (n / d).floor
r = n % d
end
count = count + dc
d = d + 2
end
if n != 1 then
count = 2 * count
end
return count
end
 
MAX = 15
@smallPrimes = getSmallPrimes(MAX)
 
def OEISA073916(n)
if isPrime(n) then
return @smallPrimes[n - 1] ** (n - 1)
end
 
count = 0
result = 0
i = 1
while count < n
if n % 2 == 1 then
# The solution for an odd (non-prime) term is always a square number
root = Math.sqrt(i)
if root * root != i then
i = i + 1
next
end
end
if getDivisorCount(i) == n then
count = count + 1
result = i
end
i = i + 1
end
return result
end
 
n = 1
while n <= MAX
print "A073916(", n, ") = ", OEISA073916(n), "\n"
n = n + 1
end</syntaxhighlight>
{{out}}
<pre>A073916(1) = 1
A073916(2) = 3
A073916(3) = 25
A073916(4) = 14
A073916(5) = 14641
A073916(6) = 44
A073916(7) = 24137569
A073916(8) = 70
A073916(9) = 1089
A073916(10) = 405
A073916(11) = 819628286980801
A073916(12) = 160
A073916(13) = 22563490300366186081
A073916(14) = 2752
A073916(15) = 9801</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func f(n {.is_prime}) {
n.prime**(n-1)
}
Line 1,453 ⟶ 2,348:
}
 
say 20.of { f(_+1) }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,464 ⟶ 2,359:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int
import "./big" for BigInt
import "./fmt" for Fmt
 
var MAX = 33
Line 1,499 ⟶ 2,394:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,545 ⟶ 2,440:
 
[[Extensible prime generator#zkl]] could be used instead.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"), pmax=25; // libGMP
p:=BI(1);
primes:=pmax.pump(List(0), p.nextPrime, "copy"); //-->(0,3,5,7,11,13,17,19,...)
Line 1,584 ⟶ 2,479:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits