Undulating numbers: Difference between revisions
Content added Content deleted
m (→{{header|J}}: efficiency note) |
(julia example) |
||
Line 391: | Line 391: | ||
{{output}} |
{{output}} |
||
Essentially as for [[#Wren|Wren]]. |
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 UndulatingIntegerSequence |
|||
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 |
|||
for (i, n) in enumerate(UndulatingInteger(ubase, 3)) |
|||
if i == 600 |
|||
print("\n\nThe 600th undulating number in base $ubase is $n.") |
|||
break |
|||
end |
|||
end |
|||
lastn = 0 |
|||
for (i, n) in enumerate(UndulatingInteger(ubase, 3)) |
|||
if 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}}== |
=={{header|Nim}}== |