Cullen and Woodall numbers: Difference between revisions
Content deleted Content added
Add Scala implementation |
|||
(10 intermediate revisions by 7 users not shown) | |||
Line 514:
<pre>Igual que la entrada de FreeBASIC.</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iostream>
#include <string>
uint32_t number, power;
void number_initialise() {
number = 0;
power = 1;
}
enum NumberType { Cullen, Woodhall };
uint32_t next_number(const NumberType& number_type) {
number += 1;
power <<= 1;
switch ( number_type ) {
case Cullen: return number * power + 1;
case Woodhall: return number * power - 1;
};
return 0;
}
void number_sequence(const uint32_t& count, const NumberType& number_type) {
std::string type = ( number_type == Cullen ) ? "Cullen" : "Woodhall";
std::cout << "The first " << count << " " << type << " numbers are:" << std::endl;
number_initialise();
for ( uint32_t index = 1; index <= count; ++index ) {
std::cout << next_number(number_type) << " ";
}
std::cout << std::endl << std::endl;
}
int main() {
number_sequence(20, Cullen);
number_sequence(20, Woodhall);
}
</syntaxhighlight>
{{ out }}
<pre>
The first 20 Cullen numbers are:
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521
The first 20 Woodhall numbers are:
1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519
</pre>
=={{header|Delphi}}==
Line 572 ⟶ 620:
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
for n = 1 to 20
write n * pow 2 n + 1 & " "
.
print ""
for n = 1 to 20
write n * pow 2 n - 1 & " "
.
print ""
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
Line 751 ⟶ 810:
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
Line 1,028 ⟶ 1,087:
{1, 141, 4713, 5795, 6611}
{2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462}</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Functions */
cullen(n):=(n*2^n)+1$
woodall(n):=(n*2^n)-1$
/* Test cases */
makelist(cullen(i),i,20);
makelist(woodall(i),i,20);
</syntaxhighlight>
{{out}}
<pre>
[3,9,25,65,161,385,897,2049,4609,10241,22529,49153,106497,229377,491521,1048577,2228225,4718593,9961473,20971521]
[1,7,23,63,159,383,895,2047,4607,10239,22527,49151,106495,229375,491519,1048575,2228223,4718591,9961471,20971519]
</pre>
=={{header|Nim}}==
{{libheader|Integers}}
<syntaxhighlight lang="Nim">import std/strformat
import integers
iterator cullenNumbers(): (int, Integer) =
var n = 1
var p = newInteger(2)
while true:
yield (n , n * p + 1)
inc n
p = p shl 1
iterator woodallNumbers(): (int, Integer) =
var n = 1
var p = newInteger(2)
while true:
yield (n , n * p - 1)
inc n
p = p shl 1
echo "First 20 Cullen numbers:"
for (n, cn) in cullenNumbers():
stdout.write &"{cn:>9}"
if n mod 5 == 0: echo()
if n == 20: break
echo "\nFirst 20 Woodall numbers:"
for (n, wn) in woodallNumbers():
stdout.write &"{wn:>9}"
if n mod 5 == 0: echo()
if n == 20: break
echo "\nFirst 5 Cullen primes (in terms of n):"
var count = 0
for (n, cn) in cullenNumbers():
if cn.isPrime:
stdout.write ' ', n
inc count
if count == 5: break
echo()
echo "\nFirst 12 Woodall primes (in terms of n):"
count = 0
for (n, wn) in woodallNumbers():
if wn.isPrime:
stdout.write ' ', n
inc count
if count == 12: break
echo()
</syntaxhighlight>
{{out}}
<pre>First 20 Cullen numbers:
3 9 25 65 161
385 897 2049 4609 10241
22529 49153 106497 229377 491521
1048577 2228225 4718593 9961473 20971521
First 20 Woodall numbers:
1 7 23 63 159
383 895 2047 4607 10239
22527 49151 106495 229375 491519
1048575 2228223 4718591 9961471 20971519
First 5 Cullen primes (in terms of n):
1 141 4713 5795 6611
First 12 Woodall primes (in terms of n):
2 3 6 30 75 81 115 123 249 362 384 462
</pre>
=={{header|PARI/GP}}==
<syntaxhighlight lang="PARI/GP">
/* Define the Cullen and Woodall number functions */
cullen(n) = n * 2^n + 1;
woodall(n) = n * 2^n - 1;
{
/* Generate the first 20 Cullen and Woodall numbers */
print(vector(20, n, cullen(n)));
print(vector(20, n, woodall(n)));
/* Find the first 5 Cullen prime numbers */
cps = [];
for(i = 1, +oo,
if(isprime(cullen(i)),
cps = concat(cps, i);
if(#cps >= 5, break);
);
);
print(cps);
/* Find the first 12 Woodall prime numbers */
wps = [];
for(i = 1, +oo,
if(isprime(woodall(i)),
wps = concat(wps, i);
if(#wps >= 12, break);
);
);
print(wps);
}
</syntaxhighlight>
{{out}}
<pre>
[3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497, 229377, 491521, 1048577, 2228225, 4718593, 9961473, 20971521]
[1, 7, 23, 63, 159, 383, 895, 2047, 4607, 10239, 22527, 49151, 106495, 229375, 491519, 1048575, 2228223, 4718591, 9961471, 20971519]
[1, 141, 4713, 5795, 6611]
[2, 3, 6, 30, 75, 81, 115, 123, 249, 362, 384, 462]
</pre>
=={{header|Perl}}==
Line 1,256 ⟶ 1,445:
=={{header|RPL}}==
≪ DUP R→B
1 ROT '''START''' SL '''NEXT'''
1 + B→R
≫ '<span style="color:blue">CULLIN</span>' STO
≪ <span style="color:blue">CULLIN</span> 2 -
≫ '<span style="color:blue">WDHAL</span>' STO
≪ {} 1 20 '''FOR''' n n <span style="color:blue">CULLIN</span> + '''NEXT''' ≫ EVAL
≪ {} 1 20 '''FOR''' n n <span style="color:blue">WDHAL</span> + '''NEXT''' ≫ EVAL
{{out}}
<pre>
Line 1,358 ⟶ 1,547:
First 12 Woodall primes in terms of n:
2 3 6 30 75 81 115 123 249 362 384 462
</pre>
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
import java.math.BigInteger
import scala.annotation.tailrec
object CullenAndWoodhall extends App {
val Certainty = 20
var number: BigInteger = _
var power: BigInteger = _
var count: Int = _
var primeIndex: Int = _
sealed trait NumberType
case object Cullen extends NumberType
case object Woodhall extends NumberType
numberSequence(20, Cullen)
numberSequence(20, Woodhall)
primeSequence(5, Cullen)
primeSequence(12, Woodhall)
def numberSequence(aCount: Int, aNumberType: NumberType): Unit = {
println(s"\nThe first $aCount $aNumberType numbers are:")
numberInitialise()
(1 to aCount).foreach { _ =>
print(nextNumber(aNumberType).toString + " ")
}
println()
}
def primeSequence(aCount: Int, aNumberType: NumberType): Unit = {
println(s"\nThe indexes of the first $aCount $aNumberType primes are:")
primeInitialise()
@tailrec
def findPrimes(): Unit = {
if (count < aCount) {
if (nextNumber(aNumberType).isProbablePrime(Certainty)) {
print(primeIndex + " ")
count += 1
}
primeIndex += 1
findPrimes()
}
}
findPrimes()
println()
}
def nextNumber(aNumberType: NumberType): BigInteger = {
number = number.add(BigInteger.ONE)
power = power.shiftLeft(1)
aNumberType match {
case Cullen => number.multiply(power).add(BigInteger.ONE)
case Woodhall => number.multiply(power).subtract(BigInteger.ONE)
}
}
def numberInitialise(): Unit = {
number = BigInteger.ZERO
power = BigInteger.ONE
}
def primeInitialise(): Unit = {
count = 0
primeIndex = 1
numberInitialise()
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 20 Cullen numbers are:
3 9 25 65 161 385 897 2049 4609 10241 22529 49153 106497 229377 491521 1048577 2228225 4718593 9961473 20971521
The first 20 Woodhall numbers are:
1 7 23 63 159 383 895 2047 4607 10239 22527 49151 106495 229375 491519 1048575 2228223 4718591 9961471 20971519
The indexes of the first 5 Cullen primes are:
1 141 4713 5794 6611
The indexes of the first 12 Woodhall primes are:
2 3 6 30 75 81 115 123 249 362 384 462
</pre>
Line 1,417 ⟶ 1,697:
{{libheader|Wren-big}}
Cullen primes limited to first 2 as very slow after that.
<syntaxhighlight lang="
var cullen = Fn.new { |n| (BigInt.one << n) * n + 1 }
Line 1,472 ⟶ 1,752:
{{libheader|Wren-gmp}}
Cullen primes still slow to emerge, just over 10 seconds overall.
<syntaxhighlight lang="
import "./gmp" for Mpz
|