Undulating numbers: Difference between revisions

Content added Content deleted
(Created Nim solution.)
Line 326:
{{output}}
Essentially as for [[#Wren|Wren]].
 
=={{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|Phix}}==