Undulating numbers: Difference between revisions

m
m (Promoted to 'full' task.)
 
(6 intermediate revisions by 4 users not shown)
Line 399:
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>
 
Line 465 ⟶ 554:
(#,&":' ',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>
 
=={{header|jq}}==
Line 1,060 ⟶ 1,411:
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>
 
67

edits