Undulating numbers: Difference between revisions

m
 
(19 intermediate revisions by 10 users not shown)
Line 1:
 
{{draft task}}
 
An '''Undulating number''' in some base is a number which has the digit form ABABAB... where A and B are digits in that base.
Line 226:
The last is: 5,252,525,252,525,252,525
which is: 8,786,648,372,058,464 in base 10
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
 
bool is_prime(uint64_t n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (uint64_t p = 5; p * p <= n; p += 4) {
if (n % p == 0)
return false;
p += 2;
if (n % p == 0)
return false;
}
return true;
}
 
class undulating_number_generator {
public:
explicit undulating_number_generator(int base) : base_(base) {}
 
uint64_t next() {
uint64_t n = 0;
for (int d = 0; d < digits_; ++d)
n = n * base_ + (d % 2 == 0 ? a_ : b_);
++b_;
if (a_ == b_)
++b_;
if (b_ == base_) {
++a_;
b_ = 0;
if (a_ == base_) {
a_ = 1;
++digits_;
}
}
return n;
}
 
private:
int base_;
int a_ = 1;
int b_ = 0;
int digits_ = 3;
};
 
std::string to_string(uint64_t n, int base) {
assert(base > 1 && base <= 16);
const char digits[] = "0123456789ABCDEF";
std::string str;
for (; n > 0; n /= base)
str += digits[n % base];
reverse(str.begin(), str.end());
return str;
}
 
void undulating(int base) {
undulating_number_generator gen(base);
uint64_t n = gen.next();
int i = 1;
uint64_t limit = base * base * base;
std::vector<uint64_t> primes;
std::cout << "3-digit undulating numbers in base " << base << ":\n";
for (; n < limit; ++i) {
std::cout << std::setw(3) << n << (i % 9 == 0 ? '\n' : ' ');
if (is_prime(n))
primes.push_back(n);
n = gen.next();
}
limit *= base;
std::cout << "\n4-digit undulating numbers in base " << base << ":\n";
for (; n < limit; ++i) {
std::cout << std::setw(4) << n << (i % 9 == 0 ? '\n' : ' ');
n = gen.next();
}
std::cout << "\n3-digit undulating numbers in base " << base
<< " which are prime:\n";
for (auto prime : primes)
std::cout << prime << ' ';
std::cout << '\n';
for (; i != 600; ++i)
n = gen.next();
std::cout << "\nThe 600th undulating number in base " << base << " is "
<< n;
if (base != 10) {
std::cout << "\nor expressed in base " << base << ": "
<< to_string(n, base);
}
std::cout << ".\n";
for (;; ++i) {
uint64_t next = gen.next();
if (next >= (1ULL << 53))
break;
n = next;
}
std::cout << "\nTotal number of undulating numbers < 2^53 in base " << base
<< ": " << i << "\nof which the largest is " << n;
if (base != 10) {
std::cout << "\nor expressed in base " << base << ": "
<< to_string(n, base);
}
std::cout << ".\n";
}
 
int main() {
undulating(10);
std::cout << '\n';
undulating(7);
}</syntaxhighlight>
 
{{out}}
<pre>
3-digit undulating numbers in base 10:
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
 
4-digit undulating numbers in base 10:
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
 
3-digit undulating numbers in base 10 which are prime:
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
 
The 600th undulating number in base 10 is 4646464646.
 
Total number of undulating numbers < 2^53 in base 10: 1125
of which the largest is 8989898989898989.
 
3-digit undulating numbers in base 7:
50 64 71 78 85 92 100 107 121
128 135 142 150 157 164 178 185 192
200 207 214 221 235 242 250 257 264
271 278 292 300 307 314 321 328 335
 
4-digit undulating numbers in base 7:
350 450 500 550 600 650 700 750 850
900 950 1000 1050 1100 1150 1250 1300 1350
1400 1450 1500 1550 1650 1700 1750 1800 1850
1900 1950 2050 2100 2150 2200 2250 2300 2350
 
3-digit undulating numbers in base 7 which are prime:
71 107 157 257 271 307
 
The 600th undulating number in base 7 is 8074217422972642
or expressed in base 7: 4646464646464646464.
 
Total number of undulating numbers < 2^53 in base 7: 603
of which the largest is 8786648372058464
or expressed in base 7: 5252525252525252525.
</pre>
 
=={{header|EasyLang}}==
{{trans|C++}}
<syntaxhighlight>
func isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
subr init
a_ = 1
b_ = 0
digits_ = 3
max_ = pow 2 53 - 1
.
func nxtundul .
for d = 1 to digits_
if d mod 2 = 1
n = n * 10 + a_
else
n = n * 10 + b_
.
if n > max_
return 0
.
.
b_ += 1
if a_ = b_
b_ += 1
.
if b_ = 10
a_ += 1
b_ = 0
if a_ = 10
a_ = 1
digits_ += 1
.
.
return n
.
init
while digits_ = 3
write nxtundul & " "
.
print "\n"
while digits_ = 4
write nxtundul & " "
.
print "\n"
init
while digits_ = 3
h = nxtundul
if isprim h = 1
write h & " "
.
.
print "\n"
init
for i to 600
h = nxtundul
.
print h
print ""
init
repeat
last = h
h = nxtundul
until h = 0
cnt += 1
.
print cnt & " " & last
</syntaxhighlight>
{{out}}
<pre>
101 121 131 141 151 161 171 181 191 202 212 232 242 252 262 272 282 292 303 313 323 343 353 363 373 383 393 404 414 424 434 454 464 474 484 494 505 515 525 535 545 565 575 585 595 606 616 626 636 646 656 676 686 696 707 717 727 737 747 757 767 787 797 808 818 828 838 848 858 868 878 898 909 919 929 939 949 959 969 979 989
 
1010 1212 1313 1414 1515 1616 1717 1818 1919 2020 2121 2323 2424 2525 2626 2727 2828 2929 3030 3131 3232 3434 3535 3636 3737 3838 3939 4040 4141 4242 4343 4545 4646 4747 4848 4949 5050 5151 5252 5353 5454 5656 5757 5858 5959 6060 6161 6262 6363 6464 6565 6767 6868 6969 7070 7171 7272 7373 7474 7575 7676 7878 7979 8080 8181 8282 8383 8484 8585 8686 8787 8989 9090 9191 9292 9393 9494 9595 9696 9797 9898
 
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
 
4646464646
 
1125 8989898989898989
</pre>
 
=={{header|J}}==
Inspection shows that there are 81 undulating numbers (base 10) for any given digit count, and 36 for base 7. (In other words two times the number of two digit combinations of the base minus the number of non-zero digits (or: the square of one less than the base).)
 
So, given:
<syntaxhighlight lang=J>require'stats'
undul=: {{ /:~ m #. y$"1(<:m)}.(,|."1)2 comb m }}
und10=: 10 undul
und7=: 7 undul
 
fmt7=: 7{{' '-.~":m&#.inv y}}</syntaxhighlight>
 
As an aside, undul could have been made more efficient, if speed was a concern, precalculating and sorting the digit pairs when the base was choosen:
 
<syntaxhighlight lang=J>undul=: {{ m {{ m #. y$"1 n}} (/:~ (<:m)}.(,|."1)2 comb m) }}</syntaxhighlight>
 
We get:
 
<syntaxhighlight lang=J> 9 9$und10 3
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
9 9$und10 4
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
(#~ 1 p:])und10 3
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
(<:600){;und10&.>3+i.>.600%81
4646464646
(#,{:)(#~ (2^53)>:]);und10&.>3+i.>.10^.2^53
1125 8989898989898989
6 7$ und7 3
50 64 71 78 85 92 100
107 121 128 135 142 150 157
164 178 185 192 200 207 214
221 235 242 250 257 264 271
278 292 300 307 314 321 328
335 50 64 71 78 85 92
6 7$ und7 4
350 450 500 550 600 650 700
750 850 900 950 1000 1050 1100
1150 1250 1300 1350 1400 1450 1500
1550 1650 1700 1750 1800 1850 1900
1950 2050 2100 2150 2200 2250 2300
2350 350 450 500 550 600 650
(#~ 1 p:])und7 3
71 107 157 257 271 307
fmt7(<:600){;und7&.>3+i.>.600%36
4646464646464646464
(#,&":' ',fmt7@{:)(#~ (2^53)>:]);und7&.>3+i.>.7^.2^53
603 5252525252525252525</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class UndulatingNumbers {
 
public static void main(String[] args) {
// Task - Part 1
UndulatingNumberIterator iterator = new UndulatingNumberIterator(3, 3, 10);
System.out.println("Three digit undulating numbers in base 10:");
int count = 0;
while ( iterator.hasNext() ) {
count += 1;
System.out.print(String.format("%3d%s", iterator.next(), ( ( count % 9 == 0 ) ? "\n" : " " )));
}
System.out.println();
// Task - Part 2
iterator = new UndulatingNumberIterator(4, 4, 10);
System.out.println("Four digit undulating numbers in base 10:");
count = 0;
while ( iterator.hasNext() ) {
count += 1;
System.out.print(String.format("%3d%s", iterator.next(), ( ( count % 9 == 0 ) ? "\n" : " " )));
}
System.out.println();
// Task - Part 3
iterator = new UndulatingNumberIterator(3, 3, 10);
System.out.println("Three digit undulating numbers in base 10 which are prime numbers:");
while ( iterator.hasNext() ) {
long undulatingNumber = iterator.next();
if ( isPrime(undulatingNumber) ) {
System.out.print(undulatingNumber + " ");
}
}
System.out.println(System.lineSeparator());
// Task - Part 4
iterator = new UndulatingNumberIterator(3, 100, 10);
count = 0;
while ( count < 599 && iterator.hasNext() ) {
count += 1;
iterator.next();
}
System.out.println("The 600th undulating number in base 10 is " + iterator.next());
System.out.println();
// Task - Part 5
final long TWO_POWER_53 = (long) Math.pow(2, 53);
final int maxDigitsBase10 = String.valueOf(TWO_POWER_53).length();
long number = 0;
long largest = 0;
count = 0;
iterator = new UndulatingNumberIterator(3, maxDigitsBase10, 10);
while ( iterator.hasNext() && ( number = iterator.next() ) < TWO_POWER_53 ) {
count += 1;
largest = number;
}
System.out.println("The number of undulating numbers in base 10 less than 2^53 is " + count);
System.out.println("The last undulating number in base 10 less than 2^53 is " + largest);
System.out.println();
// Bonus - Part 1
System.out.println("Three digit numbers, written in base 10, which are undulating in base 7:");
iterator = new UndulatingNumberIterator(3, 3, 7);
count = 0;
while ( iterator.hasNext() ) {
count += 1;
System.out.print(String.format("%3d%s", iterator.next(), ( ( count % 9 == 0 ) ? "\n" : " " )));
}
System.out.println();
// Bonus - Part 2
System.out.println("Four digit numbers, written in base 10, which are undulating in base 7:");
iterator = new UndulatingNumberIterator(4, 4, 7);
count = 0;
while ( iterator.hasNext() ) {
count += 1;
System.out.print(String.format("%3d%s", iterator.next(), ( ( count % 9 == 0 ) ? "\n" : " " )));
}
System.out.println();
// Bonus - Part 3
iterator = new UndulatingNumberIterator(3, 3, 7);
System.out.println("Three digit prime numbers, written in base 10, which are undulating in base 7:");
while ( iterator.hasNext() ) {
long undulatingNumber = iterator.next();
if ( isPrime(undulatingNumber) ) {
System.out.print(undulatingNumber + " ");
}
}
System.out.println(System.lineSeparator());
// Bonus - Part 4
iterator = new UndulatingNumberIterator(3, 100, 7);
count = 0;
while ( count < 599 && iterator.hasNext() ) {
count += 1;
iterator.next();
}
final long undulatingNumber = iterator.next();
System.out.println("The 600th undulating number in base 7 is " + convertToBase(7, undulatingNumber));
System.out.println("which is " + undulatingNumber + " written in base 10");
System.out.println();
// Task - Part 5
final int maxDigitsBase7 = convertToBase(7, TWO_POWER_53).length();
number = 0;
largest = 0;
count = 0;
iterator = new UndulatingNumberIterator(3, maxDigitsBase7, 7);
while ( iterator.hasNext() && ( number = iterator.next() ) < TWO_POWER_53 ) {
count += 1;
largest = number;
}
System.out.println("The number of undulating numbers in base 7 less than 2^53 is " + count);
System.out.println("The last undulating number in base 7 less than 2^53 is " + convertToBase(7, largest));
System.out.println("which is " + largest+ " written in base 10");
System.out.println();
}
private static final class UndulatingNumberIterator {
public UndulatingNumberIterator(int aMinDigits, int aMaxDigits, int aBase) {
minDigits = aMinDigits;
maxDigits = aMaxDigits;
base = aBase;
}
public boolean hasNext() {
return minDigits <= maxDigits;
}
public long next() {
long result = 0;
for ( int digit = 0; digit < minDigits; digit++ ) {
result = result * base + ( digit % 2 == 0 ? a : b );
}
b += 1;
if ( a == b ) {
b += 1;
}
if ( b == base ) {
b = 0;
a += 1;
if ( a == base ) {
a = 1;
minDigits += 1;
}
}
return result;
}
private int minDigits;
private int a = 1;
private int b = 0;
private final int base;
private final int maxDigits;
}
private static String convertToBase(int base, long number) {
if ( base < 2 || base > 10 ) {
throw new AssertionError("Base should be in the range: 2 << base << 10");
}
if ( number == 0 ) {
return "0";
}
StringBuilder result = new StringBuilder();
while ( number != 0 ) {
result.append(number % base);
number /= base;
}
return result.reverse().toString();
}
 
private static boolean isPrime(long number) {
if ( number < 2 ) {
return false;
}
if ( number % 2 == 0 ) {
return number == 2;
}
if ( number % 3 == 0 ) {
return number == 3;
}
for ( long p = 5; p * p <= number; p += 4 ) {
if ( number % p == 0 ) {
return false;
}
p += 2;
if ( number % p == 0 ) {
return false;
}
}
return true;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Three digit undulating numbers in base 10:
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
 
Four digit undulating numbers in base 10:
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
 
Three digit undulating numbers in base 10 which are prime numbers:
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
 
The 600th undulating number in base 10 is 4646464646
 
The number of undulating numbers in base 10 less than 2^53 is 1125
The last undulating number in base 10 less than 2^53 is 8989898989898989
 
Three digit numbers, written in base 10, which are undulating in base 7:
50 64 71 78 85 92 100 107 121
128 135 142 150 157 164 178 185 192
200 207 214 221 235 242 250 257 264
271 278 292 300 307 314 321 328 335
 
Four digit numbers, written in base 10, which are undulating in base 7:
350 450 500 550 600 650 700 750 850
900 950 1000 1050 1100 1150 1250 1300 1350
1400 1450 1500 1550 1650 1700 1750 1800 1850
1900 1950 2050 2100 2150 2200 2250 2300 2350
 
Three digit prime numbers, written in base 10, which are undulating in base 7:
71 107 157 257 271 307
 
The 600th undulating number in base 7 is 4646464646464646464
which is 8074217422972642 written in base 10
 
The number of undulating numbers in base 7 less than 2^53 is 603
The last undulating number in base 7 less than 2^53 is 5252525252525252525
which is 8786648372058464 written in base 10
</pre>
 
Line 326 ⟶ 915:
{{output}}
Essentially as for [[#Wren|Wren]].
 
=={{header|Julia}}==
<syntaxhighlight lang = "julia">using Primes
 
""" An undulating number is an integer which has the digit form ABABAB... """
struct UndulatingInteger
ubase::Int
min_digits::Int
end
 
""" Iterate undulating numbers """
function Base.iterate(u::UndulatingInteger, state = (1, 0, u.min_digits))
a, b, n = state
i = foldl((i, j) -> u.ubase * i + (iseven(j) ? b : a), 1:n, init = 0)
b += 1
if b == a
b += 1
end
if b >= u.ubase
b = 0
a += 1
if a >= u.ubase
a = 1
n += 1
end
end
return i, (a, b, n)
end
 
""" Run tests on the sequence in a given base `ubase` """
function test_undulating(ubase)
println("Three digit undulating numbers in base $ubase:")
for (i, n) in enumerate(UndulatingInteger(ubase, 3))
n >= ubase^3 - 1 && break
print(lpad(n, 5), i % 9 == 0 ? "\n" : " ")
end
println("\nFour digit undulating numbers in base $ubase:")
for (i, n) in enumerate(UndulatingInteger(ubase, 4))
n >= ubase^4 - 1 && break
print(lpad(n, 5), i % 9 == 0 ? "\n" : " ")
end
println("\nThree digit undulating numbers in base $ubase which are primes:")
for (i, n) in enumerate(Iterators.filter(isprime, UndulatingInteger(ubase, 3)))
n >= ubase^3 - 1 && break
print(n, i % 20 == 0 ? "\n" : " ")
end
lastn = 0
for (i, n) in enumerate(UndulatingInteger(ubase, 3))
if i == 600
print("\n\nThe 600th undulating number in base $ubase is $n.")
elseif n > 2^53
print("\n\nNumber of undulating numbers in base $ubase less than 2^53 is ",
i - 1, "\n with the largest such number $lastn.\n\n")
break
end
lastn = n
end
end
 
test_undulating(10)
test_undulating(7)
</syntaxhighlight>{{out}}
<pre>
Three digit undulating numbers in base 10:
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
 
Four digit undulating numbers in base 10:
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
 
Three digit undulating numbers in base 10 which are primes:
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
 
The 600th undulating number in base 10 is 4646464646.
 
Number of undulating numbers in base 10 less than 2^53 is 1125
with the largest such number 8989898989898989.
 
Three digit undulating numbers in base 7:
50 64 71 78 85 92 100 107 121
128 135 142 150 157 164 178 185 192
200 207 214 221 235 242 250 257 264
271 278 292 300 307 314 321 328 335
 
Four digit undulating numbers in base 7:
350 450 500 550 600 650 700 750 850
900 950 1000 1050 1100 1150 1250 1300 1350
1400 1450 1500 1550 1650 1700 1750 1800 1850
1900 1950 2050 2100 2150 2200 2250 2300 2350
 
Three digit undulating numbers in base 7 which are primes:
71 107 157 257 271 307
 
The 600th undulating number in base 7 is 8074217422972642.
 
Number of undulating numbers in base 7 less than 2^53 is 603
with the largest such number 8786648372058464.
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[algorithm, math, strutils]
 
func isPrime(n: Natural): 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
 
func toBase(n, base: Natural): string =
## Return the string representation of "n" in given base.
assert base in 2..10
var n = n
var s: seq[int]
if n == 0: return "0"
while n != 0:
s.add n mod base
n = n div base
result = reversed(s).join()
 
func buildNumber (a, b, n, base: int): int =
## Build an undulating number of length "n" in
## given base, returning its value in base 10.
var digits = [a, b]
var idx = 0
for i in 1..n:
result = base * result + digits[idx]
idx = 1 - idx
 
iterator undulatingNumbers(nStart, nEnd: Positive; base = 10): int =
## Yield the successive undulating numbers in given base (expressed in
## base 10), starting with "nStart" digits and ending with "nEnd" digits.
assert nStart >= 3, "need at least three digits."
var n = nStart
while n <= nEnd :
for a in 1..<base:
for b in 0..<base:
if b == a: continue
yield buildNumber(a, b, n, base)
inc n
 
### Task - Part 1 ###
 
echo "Three digits undulating numbers in base 10:"
var count = 0
for unum in undulatingNumbers(3, 3):
inc count
stdout.write unum
stdout.write if count mod 9 == 0: '\n' else: ' '
 
### Task - Part 2 ###
 
echo "\nFour digits undulating numbers in base 10:"
count = 0
for unum in undulatingNumbers(4, 4):
inc count
stdout.write unum
stdout.write if count mod 9 == 0: '\n' else: ' '
 
### Task - Part 3 ###
 
echo "\nThree digits undulating numbers in base 10 which are primes:"
count = 0
for unum in undulatingNumbers(3, 3):
if unum.isPrime:
inc count
stdout.write unum
stdout.write if count == 15: '\n' else: ' '
 
### Task - Part 4 ###
 
count = 0
for unum in undulatingNumbers(3, 10):
inc count
if count == 600:
echo "\nThe 600th undulating number in base 10 is ", unum
break
 
### Task - Part 5 ###
 
const N = 2^53
count = (D10 - 3) * 81 # For each number of digits, there are 9 × 9 undulating numbers.
count = (D10 - 3) * 81
var last: int
for unum in undulatingNumbers(D10, D10):
if unum < N:
inc count
last = unum
else: break
echo "\nNumber of undulating numbers in base 10 less than 2^53: ", count
echo "The last undulating number in base 10 less than 2^53 is ", last
 
### Bonus - Part 1 ###
echo "\nThree digits undulating numbers in base 7 (shown in base 10):"
count = 0
for unum in undulatingNumbers(3, 3, 7):
inc count
stdout.write align($unum, 3)
stdout.write if count mod 9 == 0: '\n' else: ' '
 
### Bonus - Part 2 ###
echo "\nFour digits undulating numbers in base 7 (shown in base 10):"
count = 0
for unum in undulatingNumbers(4, 4, 7):
inc count
stdout.write align($unum, 4)
stdout.write if count mod 9 == 0: '\n' else: ' '
 
### Bonus - Part 3 ###
 
echo "\nThree digits undulating numbers in base 7 which are primes:"
count = 0
for unum in undulatingNumbers(3, 3, 7):
if unum.isPrime:
inc count
stdout.write unum
stdout.write if count == 6: '\n' else: ' '
 
### Bonus - Part 4 ###
 
count = 0
for unum in undulatingNumbers(3, 19, 7):
inc count
if count == 600:
echo "\nThe 600th undulating number in base 7 is ", unum.toBase(7)
echo "which is ", unum, " in base 10."
break
 
### Bonus - Part 5 ###
 
const D7 = int(ln(N.toFloat) / ln(7.0)) + 1
count = (D7 - 3) * 36 # For each number of digits, there are 6 × 6 undulating numbers.
last = 0
for unum in undulatingNumbers(D7, D7, 7):
if unum < N:
inc count
last = unum
else: break
echo "\nNumber of undulating numbers in base 7 less than 2^53: ", count
echo "The last undulating number in base 7 less than 2^53 is ", last.toBase(7)
echo "which is ", last, " in base 10."
</syntaxhighlight>
 
{{out}}
<pre>Three digits undulating numbers in base 10:
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
 
Four digits undulating numbers in base 10:
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
 
Three digits undulating numbers in base 10 which are primes:
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
 
The 600th undulating number in base 10 is 4646464646
 
Number of undulating numbers in base 10 less than 2^53: 1125
The last undulating number in base 10 less than 2^53 is 8989898989898989
 
Three digits undulating numbers in base 7 (shown in base 10):
50 64 71 78 85 92 100 107 121
128 135 142 150 157 164 178 185 192
200 207 214 221 235 242 250 257 264
271 278 292 300 307 314 321 328 335
 
Four digits undulating numbers in base 7 (shown in base 10):
350 450 500 550 600 650 700 750 850
900 950 1000 1050 1100 1150 1250 1300 1350
1400 1450 1500 1550 1650 1700 1750 1800 1850
1900 1950 2050 2100 2150 2200 2250 2300 2350
 
Three digits undulating numbers in base 7 which are primes:
71 107 157 257 271 307
 
The 600th undulating number in base 7 is 4646464646464646464
which is 8074217422972642 in base 10.
 
Number of undulating numbers in base 7 less than 2^53: 603
The last undulating number in base 7 less than 2^53 is 5252525252525252525
which is 8786648372058464 in base 10.
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>
use v5.36;
use bigint;
use experimental <builtin for_list>;
use ntheory <is_prime vecfirstidx>;
 
sub X ($a, $b) { my @c; for my $aa (0..$a) { for my $bb (0..$b) { push @c, $aa, $bb } } @c }
sub table ($c, @V) { my $t = $c * (my $w = 6); ( sprintf( ('%'.$w.'d')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }
 
my $max = 2**53;
 
my(@pairs,@U);
for my($i,$j) ( X(9,9) ) { push @pairs, $i . $j unless $i == 0 || $i == $j }
for my $l (3..length $max) {
if (0 == $l%2) { push @U, "$_"x( $l /2) for @pairs }
else { push @U, "$_"x(($l+1)/2) and chop $U[-1] for @pairs }
}
 
say "All 3 digit undulating numbers:"; say table 9, grep { 3 == length $_ } @U;
say "All 3 digit undulating primes:"; say table 9, grep { 3 == length $_ and is_prime $_ } @U;
say "All 4 digit undulating numbers:"; say table 9, grep { 4 == length $_ } @U;
 
my $fmt = "%34s: %d\n";
printf $fmt, 'The 600th undulating number is', $U[599];
printf $fmt, 'Undulating numbers less than 2**53', (my $i = vecfirstidx { $_ >= $max } @U);
printf $fmt, 'That number is', $U[$i-1];</syntaxhighlight>
{{out}}
<pre>
All 3 digit undulating numbers:
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
 
All 3 digit undulating primes:
101 131 151 181 191 313 353 373 383
727 757 787 797 919 929
 
All 4 digit undulating numbers:
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
 
The 600th undulating number is: 4646464646
Undulating numbers less than 2**53: 1125
That number is: 8989898989898989
</pre>
 
=={{header|Phix}}==
Line 410 ⟶ 1,378:
undulating $_, 600 for 10, 7;</syntaxhighlight>
You may [https://tio.run/##nVTbbtpAEH0uX3GCHMU2xoJCU9UuCXnpF/BQqaTROl7AaL12fclFyN9OZ9c2mBSpaZHwZXfmnDMzx5vyTFzv93kZoJRhKVgRyTVMI2A5d7CUFnY9APErliKKowIzfLRtU73HafJMr58mFoYY@02YuQzyXyXLuDMvJ/SfWhSj4Wx99aECV0mGsev@1Ev4jtHheXhDEMxZBg0z/SR/KRCtEGA2A/ObVYJ30zLfmAw2Gk4MKMiu6eiZWcfgaR2slT@RJNaJI64uxJOlsip1ydkr@kt5JwQmCKM1NaDTJlnGAc9yRBIaSeN5fU3qruLCvLqchFeWq1BUxUpylhRJ5uGL/wZ@@l/w01P46REe75f/vIkeN1C1p1kU8/OE1Ld5sz3Thawznnqw3Sgf6vWa8XzddWIrbjxy4KUsKyImWuxlKR8JWI1nXsqGgqUpl6GnCrNcLnicH1xmbB0YYSK5shfhfWOCvKV2RZKkB@soduUthd6u1QjEqFnkD2NL01cBAxgP9ydOOLN92W77vQ8ocYP6s7i9hVkLItRFVnIwGUKwvICFiwvaJCxtQY@yDr6smnudqUGOOcZ2MMBbKy42HDtZ/TlHNTXjODZEuaf1y@H4vn90GzWEv6QZz3Meng4aHnZtgqsW6lPAqvpEJiihCaOPcDzq2neRFEy0KpLVXy2Mr@oM2anzo6Ju7QbEWnUlrhpLFlSrYNmaU0dUOX1HV2STwH8pyH5fQVWv11FuPDi4Ho3qk4oM9tnf738D Try it online!]
 
=={{header|RPL}}==
Once found the direct formula, the code is fairly simple to write.
{{works with|HP|49}}
« 1 - → n
« n 81 MOD 9 / FLOOR 1 + <span style="color:grey">@ A = mod(N-1,81)//9 + 1</span>
n 9 MOD DUP2 ≤ + <span style="color:grey">@ B = mod(N-1,9) + (A ≤ mod(N-1,9))</span>
→STR +
n 81 / FLOOR 3 + SWAP
'''WHILE''' DUP2 SIZE > '''REPEAT''' DUP + '''END'''
1 ROT SUB STR→ <span style="color:grey">@ reduce # of digits to N//81 + 3</span>
» » '<span style="color:blue">N→UND</span>' STO
« « n <span style="color:blue">N→UND</span> » 'n' 1 81 1 SEQ
« n <span style="color:blue">N→UND</span> » 'n' 82 162 1 SEQ
{ }
1 4 PICK SIZE '''FOR''' j
PICK3 j GET
'''IF''' DUP ISPRIME? '''THEN''' + '''ELSE''' DROP '''END'''
'''NEXT'''
625 <span style="color:blue">N→UND</span>
2 53 ^ 0
'''DO''' 1 + '''UNTIL''' DUP2 <span style="color:blue">N→UND</span> < '''END'''
1 - NIP DUP <span style="color:blue">N→UND</span>
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
6: { 101 121 131 141 151 161 171 181 191 202 212 232 242 252 262 272 282 292 303 313 323 343 353 363 373 383 393 404 414 424 434 454 464 474 484 494 505 515 525 535 545 565 575 585 595 606 616 626 636 646 656 676 686 696 707 717 727 737 747 757 767 787 797 808 818 828 838 848 858 868 878 898 909 919 929 939 949 959 969 979 989 }
5: { 1010 1212 1313 1414 1515 1616 1717 1818 1919 2020 2121 2323 2424 2525 2626 2727 2828 2929 3030 3131 3232 3434 3535 3636 3737 3838 3939 4040 4141 4242 4343 4545 4646 4747 4848 4949 5050 5151 5252 5353 5454 5656 5757 5858 5959 6060 6161 6262 6363 6464 6565 6767 6868 6969 7070 7171 7272 7373 7474 7575 7676 7878 7979 8080 8181 8282 8383 8484 8585 8686 8787 8989 9090 9191 9292 9393 9494 9595 9696 9797 9898 }
4: { 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 }
3: 4646464646
2: 1125
1: 8989898989898989
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
// radix_fmt = "1.0"
 
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
if n % 2 == 0 {
return n == 2;
}
if n % 3 == 0 {
return n == 3;
}
let mut p: u64 = 5;
while p * p <= n {
if n % p == 0 {
return false;
}
p += 2;
if n % p == 0 {
return false;
}
p += 4;
}
true
}
 
fn undulating_numbers(base: u64) -> impl std::iter::Iterator<Item = u64> {
let mut a = 1;
let mut b = 0;
let mut digits = 3;
std::iter::from_fn(move || {
let mut n = 0;
for d in 0..digits {
n = n * base + if d % 2 == 0 { a } else { b };
}
b += 1;
if a == b {
b += 1;
}
if b == base {
a += 1;
b = 0;
if a == base {
a = 1;
digits += 1;
}
}
Some(n)
})
}
 
fn undulating(base: u64) {
let mut count = 0;
let limit3 = base * base * base;
let limit4 = base * limit3;
let mut u3 = Vec::new();
let mut u4 = Vec::new();
let mut umax = 0;
let mut u600 = 0;
 
for n in undulating_numbers(base).take_while(|x| *x < 1u64 << 53) {
if n < limit3 {
u3.push(n);
} else if n < limit4 {
u4.push(n);
}
count += 1;
umax = n;
if count == 600 {
u600 = n;
}
}
 
println!("3-digit undulating numbers in base {}:", base);
for (i, n) in u3.iter().enumerate() {
print!("{:3}{}", n, if (i + 1) % 9 == 0 { '\n' } else { ' ' });
}
 
println!("\n4-digit undulating numbers in base {}:", base);
for (i, n) in u4.iter().enumerate() {
print!("{:4}{}", n, if (i + 1) % 9 == 0 { '\n' } else { ' ' });
}
 
println!(
"\n3-digit undulating numbers in base {} which are prime:",
base
);
for n in u3 {
if is_prime(n) {
print!("{} ", n);
}
}
println!();
 
print!("\nThe 600th undulating number in base {} is {}", base, u600);
if base != 10 {
print!(
"\nor expressed in base {}: {}",
base,
radix_fmt::radix(u600, base as u8)
);
}
println!(".");
 
print!(
"\nTotal number of undulating numbers < 2^53 in base {}: {}\nof which the largest is {}",
base, count, umax
);
if base != 10 {
print!(
"\nor expressed in base {}: {}",
base,
radix_fmt::radix(umax, base as u8)
);
}
println!(".");
}
 
fn main() {
undulating(10);
println!();
undulating(7);
}</syntaxhighlight>
 
{{out}}
<pre>
3-digit undulating numbers in base 10:
101 121 131 141 151 161 171 181 191
202 212 232 242 252 262 272 282 292
303 313 323 343 353 363 373 383 393
404 414 424 434 454 464 474 484 494
505 515 525 535 545 565 575 585 595
606 616 626 636 646 656 676 686 696
707 717 727 737 747 757 767 787 797
808 818 828 838 848 858 868 878 898
909 919 929 939 949 959 969 979 989
 
4-digit undulating numbers in base 10:
1010 1212 1313 1414 1515 1616 1717 1818 1919
2020 2121 2323 2424 2525 2626 2727 2828 2929
3030 3131 3232 3434 3535 3636 3737 3838 3939
4040 4141 4242 4343 4545 4646 4747 4848 4949
5050 5151 5252 5353 5454 5656 5757 5858 5959
6060 6161 6262 6363 6464 6565 6767 6868 6969
7070 7171 7272 7373 7474 7575 7676 7878 7979
8080 8181 8282 8383 8484 8585 8686 8787 8989
9090 9191 9292 9393 9494 9595 9696 9797 9898
 
3-digit undulating numbers in base 10 which are prime:
101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
 
The 600th undulating number in base 10 is 4646464646.
 
Total number of undulating numbers < 2^53 in base 10: 1125
of which the largest is 8989898989898989.
 
3-digit undulating numbers in base 7:
50 64 71 78 85 92 100 107 121
128 135 142 150 157 164 178 185 192
200 207 214 221 235 242 250 257 264
271 278 292 300 307 314 321 328 335
 
4-digit undulating numbers in base 7:
350 450 500 550 600 650 700 750 850
900 950 1000 1050 1100 1150 1250 1300 1350
1400 1450 1500 1550 1650 1700 1750 1800 1850
1900 1950 2050 2100 2150 2200 2250 2300 2350
 
3-digit undulating numbers in base 7 which are prime:
71 107 157 257 271 307
 
The 600th undulating number in base 7 is 8074217422972642
or expressed in base 7: 4646464646464646464.
 
Total number of undulating numbers < 2^53 in base 7: 603
of which the largest is 8786648372058464
or expressed in base 7: 5252525252525252525.
</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
More of a script than a program, and only for base 10 as there's no built-in support for other radixes in Uiua yet.
<syntaxhighlight lang="Uiua">
# Generate all distinct two digit numbers as strings.
≡(°⋕)▽≡(≠0◿11).↘10⇡100
&p≡(⋕⊂⟜⊢) . &pf "Three digits: "
&p≡(⋕⊂.) . &pf "Four digits: "
≡(⋕⊂⟜⊢) . &pf "Three digit primes: "
# Primes by sieve.
⇌◌⍢(▽≠0◿⊃⊢(.↘1)⟜(⊂⊢)|>0⧻)↘2⇡1000[]
&p▽:⟜(/+⍉⊞⌕):
# Collect all 3 and 4 digit numbers.
⊂⊃≡(⋕⊂⟜⊢)≡(⋕⊂.)
# Double-up last two digits of each and remove the ensuing duplicates.
F ← ◴⊂⟜≡(⋕⊂⟜(↙¯2)°⋕)
# Repeat until length is okay.
⍢(F|<600⧻)
&p⊡599 . &pf "Six hundredth: "
# Repeat until last is larger than target.
⍢(F|<ⁿ53 2⊡¯1)
&p⧻.▽<ⁿ53 2. &pf "Count less than 2^53: "
&p⊡¯1⊏⍏. &pf "Last one less than 2^53: "
 
</syntaxhighlight>
{{out}}
<pre>
Three digits: [101 121 131 141 151 161 171 181 191 202 212 232 242 252 262 272 282 292 303 313 323 343 353 363 373 383 393 404 414 424 434 454 464 474 484 494 505 515 525 535 545 565 575 585 595 606 616 626 636 646 656 676 686 696 707 717 727 737 747 757 767 787 797 808 818 828 838 848 858 868 878 898 909 919 929 939 949 959 969 979 989]
Four digits: [1010 1212 1313 1414 1515 1616 1717 1818 1919 2020 2121 2323 2424 2525 2626 2727 2828 2929 3030 3131 3232 3434 3535 3636 3737 3838 3939 4040 4141 4242 4343 4545 4646 4747 4848 4949 5050 5151 5252 5353 5454 5656 5757 5858 5959 6060 6161 6262 6363 6464 6565 6767 6868 6969 7070 7171 7272 7373 7474 7575 7676 7878 7979 8080 8181 8282 8383 8484 8585 8686 8787 8989 9090 9191 9292 9393 9494 9595 9696 9797 9898]
Three digit primes: [101 131 151 181 191 313 353 373 383 727 757 787 797 919 929]
Six hundredth: 4646464646
Count less than 2^53: 1125
Last one less than 2^53: 8989898989898989
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt, Conv
import "./math" for Int
 
73

edits