Magic numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (New draft task and Raku example) |
(Added Wren) |
||
Line 85: | Line 85: | ||
All magic numbers that are pan-digital in 0 through 9 with no repeats: 3816547290</pre> |
All magic numbers that are pan-digital in 0 through 9 with no repeats: 3816547290</pre> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-big}} |
|||
{{libheader|Wren-fmt}} |
|||
This is based on the Python code in the Wikipedia article. |
|||
<syntaxhighlight lang="ecmascript">import "./big" for BigInt |
|||
import "./fmt" for Fmt |
|||
var polydivisible = Fn.new { |
|||
var numbers = [] |
|||
var previous = (1..9).toList |
|||
var new = [] |
|||
var digits = 2 |
|||
while (previous.count > 0) { |
|||
numbers.add(previous) |
|||
for (n in previous) { |
|||
for (j in 0..9) { |
|||
var number = BigInt.ten * n + j |
|||
if (number % digits == 0) new.add(number) |
|||
} |
|||
} |
|||
previous = new |
|||
new = [] |
|||
digits = digits + 1 |
|||
} |
|||
return numbers |
|||
} |
|||
var numbers = polydivisible.call() |
|||
numbers[0].add(BigInt.zero) // include zero |
|||
var total = numbers.reduce(0) { |acc, number| acc + number.count } |
|||
Fmt.print("There are $,d magic numbers in total.", total) |
|||
var largest = numbers[-1][-1] |
|||
Fmt.print("\nThe largest is $,i.", largest) |
|||
System.print("\nThere are:") |
|||
for (i in 0...numbers.count) { |
|||
Fmt.print("$,5d with $2d digit$s", numbers[i].count, i+1, (i == 0) ? "" : "s") |
|||
} |
|||
var pd19 = [] |
|||
for (n in numbers[8]) { |
|||
var s = n.toString |
|||
var pandigital = true |
|||
for (i in 1..9) { |
|||
if (!s.contains(i.toString)) { |
|||
pandigital = false |
|||
break |
|||
} |
|||
} |
|||
if (pandigital) pd19.add(n) |
|||
} |
|||
System.print("\nAll magic numbers that are pan-digital in 1 through 9 with no repeats: ") |
|||
Fmt.print("$,i", pd19) |
|||
var pd09 = [] |
|||
for (n in numbers[9]) { |
|||
var s = n.toString |
|||
var pandigital = true |
|||
for (i in 0..9) { |
|||
if (!s.contains(i.toString)) { |
|||
pandigital = false |
|||
break |
|||
} |
|||
} |
|||
if (pandigital) pd09.add(n) |
|||
} |
|||
System.print("\nAll magic numbers that are pan-digital in 0 through 9 with no repeats: ") |
|||
Fmt.print("$,i", pd09)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
There are 20,457 magic numbers in total. |
|||
The largest is 3,608,528,850,368,400,786,036,725. |
|||
There are: |
|||
10 with 1 digit |
|||
45 with 2 digits |
|||
150 with 3 digits |
|||
375 with 4 digits |
|||
750 with 5 digits |
|||
1,200 with 6 digits |
|||
1,713 with 7 digits |
|||
2,227 with 8 digits |
|||
2,492 with 9 digits |
|||
2,492 with 10 digits |
|||
2,225 with 11 digits |
|||
2,041 with 12 digits |
|||
1,575 with 13 digits |
|||
1,132 with 14 digits |
|||
770 with 15 digits |
|||
571 with 16 digits |
|||
335 with 17 digits |
|||
180 with 18 digits |
|||
90 with 19 digits |
|||
44 with 20 digits |
|||
18 with 21 digits |
|||
12 with 22 digits |
|||
6 with 23 digits |
|||
3 with 24 digits |
|||
1 with 25 digits |
|||
All magic numbers that are pan-digital in 1 through 9 with no repeats: |
|||
381,654,729 |
|||
All magic numbers that are pan-digital in 0 through 9 with no repeats: |
|||
3,816,547,290 |
|||
</pre> |
Revision as of 00:27, 1 February 2023
Magic numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Magic numbers are polydivisible numbers in base 10. A polydivisible number is a number where the first n digits are evenly divisible by n.
E.G. The number 1868587 is a magic number (is polydivisible in base 10.)
1 ÷ 1 = 1 18 ÷ 2 = 9 186 ÷ 3 = 62 1868 ÷ 4 = 467 18685 ÷ 5 = 3737 186858 ÷ 6 = 31143 1868587 ÷ 7 = 266941
There is a finite number of magic numbers.
- Task
- Write a routine (subroutine, function, procedure, generator, whatever it may be called in your language) to find magic numbers.
- Use that routine to find and display how many magic numbers exist.
- Use that routine to find and display the largest possible magic number.
- Count and display how many magic numbers have 1 digit, 2 digits, 3 digits, ... for all magic numbers.
- Find and display all of the magic numbers that are minimally pandigital in 1 through 9. (Contains each digit but only once.)
- Find and display all of the magic numbers that are minimally pandigital in 0 through 9.
Zero (0) may or may not be included as a magic number. For this task, include zero.
- See also
Raku
my \Δ = $ = 1;
my @magic = flat 0, [1..9], {last if .not; ++Δ; [(.flat X~ 0..9).grep: * %% Δ]}…*;
put "There are {@magic.eager.elems} magic numbers in total.";
put "\nThe largest is {@magic.tail}.";
put "\nThere are:";
put "{(+.value).fmt: "%4d"} with {.key.fmt: "%2d"} digit{1 == +.key ?? '' !! 's'}"
for sort @magic.classify: {.chars};
{
my $pan-digital = ($_..9).join.comb.Bag;
put "\nAll magic numbers that are pan-digital in $_ through 9 with no repeats: " ~
@magic.grep( { .comb.Bag eqv $pan-digital } );
} for 1, 0;
- Output:
There are 20457 magic numbers in total. The largest is 3608528850368400786036725. There are: 10 with 1 digit 45 with 2 digits 150 with 3 digits 375 with 4 digits 750 with 5 digits 1200 with 6 digits 1713 with 7 digits 2227 with 8 digits 2492 with 9 digits 2492 with 10 digits 2225 with 11 digits 2041 with 12 digits 1575 with 13 digits 1132 with 14 digits 770 with 15 digits 571 with 16 digits 335 with 17 digits 180 with 18 digits 90 with 19 digits 44 with 20 digits 18 with 21 digits 12 with 22 digits 6 with 23 digits 3 with 24 digits 1 with 25 digits All magic numbers that are pan-digital in 1 through 9 with no repeats: 381654729 All magic numbers that are pan-digital in 0 through 9 with no repeats: 3816547290
Wren
This is based on the Python code in the Wikipedia article.
import "./big" for BigInt
import "./fmt" for Fmt
var polydivisible = Fn.new {
var numbers = []
var previous = (1..9).toList
var new = []
var digits = 2
while (previous.count > 0) {
numbers.add(previous)
for (n in previous) {
for (j in 0..9) {
var number = BigInt.ten * n + j
if (number % digits == 0) new.add(number)
}
}
previous = new
new = []
digits = digits + 1
}
return numbers
}
var numbers = polydivisible.call()
numbers[0].add(BigInt.zero) // include zero
var total = numbers.reduce(0) { |acc, number| acc + number.count }
Fmt.print("There are $,d magic numbers in total.", total)
var largest = numbers[-1][-1]
Fmt.print("\nThe largest is $,i.", largest)
System.print("\nThere are:")
for (i in 0...numbers.count) {
Fmt.print("$,5d with $2d digit$s", numbers[i].count, i+1, (i == 0) ? "" : "s")
}
var pd19 = []
for (n in numbers[8]) {
var s = n.toString
var pandigital = true
for (i in 1..9) {
if (!s.contains(i.toString)) {
pandigital = false
break
}
}
if (pandigital) pd19.add(n)
}
System.print("\nAll magic numbers that are pan-digital in 1 through 9 with no repeats: ")
Fmt.print("$,i", pd19)
var pd09 = []
for (n in numbers[9]) {
var s = n.toString
var pandigital = true
for (i in 0..9) {
if (!s.contains(i.toString)) {
pandigital = false
break
}
}
if (pandigital) pd09.add(n)
}
System.print("\nAll magic numbers that are pan-digital in 0 through 9 with no repeats: ")
Fmt.print("$,i", pd09)
- Output:
There are 20,457 magic numbers in total. The largest is 3,608,528,850,368,400,786,036,725. There are: 10 with 1 digit 45 with 2 digits 150 with 3 digits 375 with 4 digits 750 with 5 digits 1,200 with 6 digits 1,713 with 7 digits 2,227 with 8 digits 2,492 with 9 digits 2,492 with 10 digits 2,225 with 11 digits 2,041 with 12 digits 1,575 with 13 digits 1,132 with 14 digits 770 with 15 digits 571 with 16 digits 335 with 17 digits 180 with 18 digits 90 with 19 digits 44 with 20 digits 18 with 21 digits 12 with 22 digits 6 with 23 digits 3 with 24 digits 1 with 25 digits All magic numbers that are pan-digital in 1 through 9 with no repeats: 381,654,729 All magic numbers that are pan-digital in 0 through 9 with no repeats: 3,816,547,290