Colorful numbers: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: import) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 36: | Line 36: | ||
=={{header|C}}== |
=={{header|C}}== |
||
The <code>count_colorful</code> function is based on [[#Phix|Phix]]. |
The <code>count_colorful</code> function is based on [[#Phix|Phix]]. |
||
< |
<syntaxhighlight lang="c">#include <locale.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 126: | Line 126: | ||
(end - start + 0.0) / CLOCKS_PER_SEC); |
(end - start + 0.0) / CLOCKS_PER_SEC); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 158: | Line 158: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: assocs grouping grouping.extras io kernel literals math |
||
math.combinatorics math.ranges prettyprint project-euler.common |
math.combinatorics math.ranges prettyprint project-euler.common |
||
sequences sequences.extras sets ; |
sequences sequences.extras sets ; |
||
Line 187: | Line 187: | ||
"Count of colorful numbers by number of digits:" print |
"Count of colorful numbers by number of digits:" print |
||
8 [1..b] [ oom-count ] zip-with dup . |
8 [1..b] [ oom-count ] zip-with dup . |
||
"Total: " write values sum .</ |
"Total: " write values sum .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 219: | Line 219: | ||
{{trans|Phix}} |
{{trans|Phix}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 338: | Line 338: | ||
} |
} |
||
fmt.Printf("\nTotal colorful numbers: %s\n", rcu.Commatize(sum)) |
fmt.Printf("\nTotal colorful numbers: %s\n", rcu.Commatize(sum)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 369: | Line 369: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
{{incorrect|Haskell|10 is not a Colorful number; nor is 98765432: <nowiki>6×5×4 == 5×4×3×2 == 120</nowiki>}} |
{{incorrect|Haskell|10 is not a Colorful number; nor is 98765432: <nowiki>6×5×4 == 5×4×3×2 == 120</nowiki>}} |
||
< |
<syntaxhighlight lang="haskell">import Data.List ( nub ) |
||
import Data.List.Split ( divvy ) |
import Data.List.Split ( divvy ) |
||
import Data.Char ( digitToInt ) |
import Data.Char ( digitToInt ) |
||
Line 391: | Line 391: | ||
solution2 :: Integer |
solution2 :: Integer |
||
solution2 = head $ filter isColourful [98765432, 98765431 ..]</ |
solution2 = head $ filter isColourful [98765432, 98765431 ..]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[0,1,2,3,4,5,6,7,8,9,10,23,24,25,26,27,28,29,32,34,35,36,37,38,39,42,43,45,46,47,48,49,52,53,54,56,57,58,59,62,63,64,65,67,68,69,72,73,74,75,76,78,79,82,83,84,85,86,87,89,92,93,94,95,96,97,98](solution1) |
<pre>[0,1,2,3,4,5,6,7,8,9,10,23,24,25,26,27,28,29,32,34,35,36,37,38,39,42,43,45,46,47,48,49,52,53,54,56,57,58,59,62,63,64,65,67,68,69,72,73,74,75,76,78,79,82,83,84,85,86,87,89,92,93,94,95,96,97,98](solution1) |
||
Line 398: | Line 398: | ||
=={{header|Haskell (alternate version)}}== |
=={{header|Haskell (alternate version)}}== |
||
An alternate Haskell version, which some may consider to be in a more idiomatic style. No attempt at optimization has been made, so we don't attempt the stretch goals. |
An alternate Haskell version, which some may consider to be in a more idiomatic style. No attempt at optimization has been made, so we don't attempt the stretch goals. |
||
< |
<syntaxhighlight lang="haskell">import Data.List (inits, nub, tails, unfoldr) |
||
-- Non-empty subsequences containing only consecutive elements from the |
-- Non-empty subsequences containing only consecutive elements from the |
||
Line 429: | Line 429: | ||
let start = 98765432 |
let start = 98765432 |
||
largest = head $ dropWhile (not . isColorful) [start, start-1 ..] |
largest = head $ dropWhile (not . isColorful) [start, start-1 ..] |
||
putStrLn $ "Largest colorful number: " ++ show largest</ |
putStrLn $ "Largest colorful number: " ++ show largest</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 439: | Line 439: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j"> colorful=: {{(-:~.);<@(*/\)\. 10 #.inv y}}"0 |
||
I.colorful i.100 |
I.colorful i.100 |
||
0 1 2 3 4 5 6 7 8 9 23 24 25 26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49 52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75 76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98 |
0 1 2 3 4 5 6 7 8 9 23 24 25 26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49 52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75 76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98 |
||
Line 456: | Line 456: | ||
7 14256 |
7 14256 |
||
#C |
#C |
||
57256</ |
57256</syntaxhighlight> |
||
(Note that 0, here is a different order of magnitude than 1.) |
(Note that 0, here is a different order of magnitude than 1.) |
||
Line 462: | Line 462: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="java">public class ColorfulNumbers { |
||
private int count[] = new int[8]; |
private int count[] = new int[8]; |
||
private boolean used[] = new boolean[10]; |
private boolean used[] = new boolean[10]; |
||
Line 545: | Line 545: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 574: | Line 574: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">largest = 0 |
||
function iscolorful(n, base=10) |
function iscolorful(n, base=10) |
||
Line 610: | Line 610: | ||
testcolorfuls() |
testcolorfuls() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Colorful numbers for 1:25, 26:50, 51:75, and 76:100: |
Colorful numbers for 1:25, 26:50, 51:75, and 76:100: |
||
Line 630: | Line 630: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[ColorfulNumberQ] |
||
ColorfulNumberQ[n_Integer?NonNegative] := Module[{digs, parts}, |
ColorfulNumberQ[n_Integer?NonNegative] := Module[{digs, parts}, |
||
If[n > 98765432, |
If[n > 98765432, |
||
Line 652: | Line 652: | ||
Tally[IntegerDigits/*Length /@ cns] // Grid |
Tally[IntegerDigits/*Length /@ cns] // Grid |
||
Length[cns]</ |
Length[cns]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 2 3 4 5 6 7 8 |
<pre>1 2 3 4 5 6 7 8 |
||
Line 679: | Line 679: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 710: | Line 710: | ||
my $total= 10; |
my $total= 10; |
||
map { is_colorful(join '', @$_) and $total++ } map { permutations $_ } combinations [2..9], $_ for 2..8; |
map { is_colorful(join '', @$_) and $total++ } map { permutations $_ } combinations [2..9], $_ for 2..8; |
||
say "Total colorful numbers: $total";</ |
say "Total colorful numbers: $total";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Colorful numbers less than 100: |
<pre>Colorful numbers less than 100: |
||
Line 728: | Line 728: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/colourful.htm here]. |
You can run this online [http://phix.x10.mx/p2js/colourful.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">colourful</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">colourful</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> |
||
Line 792: | Line 792: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nTotal colourful numbers: %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nTotal colourful numbers: %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">))</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 820: | Line 820: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">colorful_number(N) => |
||
N < 10 ; |
N < 10 ; |
||
(X = N.to_string, |
(X = N.to_string, |
||
Line 830: | Line 830: | ||
distinct(L) => |
distinct(L) => |
||
L.len == L.remove_dups.len.</ |
L.len == L.remove_dups.len.</syntaxhighlight> |
||
'''All colorful numbers <= 100.''' |
'''All colorful numbers <= 100.''' |
||
< |
<syntaxhighlight lang="picat">main => |
||
Colorful = [N : N in 0..100, colorful_number(N)], |
Colorful = [N : N in 0..100, colorful_number(N)], |
||
Len = Colorful.len, |
Len = Colorful.len, |
||
Line 840: | Line 840: | ||
end, |
end, |
||
nl, |
nl, |
||
println(len=Len)</ |
println(len=Len)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 853: | Line 853: | ||
'''Largest colorful number.''' |
'''Largest colorful number.''' |
||
< |
<syntaxhighlight lang="picat">main => |
||
N = 98765431, |
N = 98765431, |
||
Found = false, |
Found = false, |
||
Line 862: | Line 862: | ||
end, |
end, |
||
N := N - 1 |
N := N - 1 |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 868: | Line 868: | ||
'''Count of colorful numbers in each magnitude and of total colorful numbers.''' |
'''Count of colorful numbers in each magnitude and of total colorful numbers.''' |
||
< |
<syntaxhighlight lang="picat">main => |
||
TotalC = 0, |
TotalC = 0, |
||
foreach(I in 1..8) |
foreach(I in 1..8) |
||
Line 887: | Line 887: | ||
% For N=3: lb=123 and ub=987 |
% For N=3: lb=123 and ub=987 |
||
lb(N) = cond(N < 2, 0, [I.to_string : I in 1..N].join('').to_int). |
lb(N) = cond(N < 2, 0, [I.to_string : I in 1..N].join('').to_int). |
||
ub(N) = [I.to_string : I in 9..-1..9-N+1].join('').to_int.</ |
ub(N) = [I.to_string : I in 9..-1..9-N+1].join('').to_int.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 901: | Line 901: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">from math import prod |
||
largest = [0] |
largest = [0] |
||
Line 939: | Line 939: | ||
print(f'The largest possible colorful number is {largest[0]}.') |
print(f'The largest possible colorful number is {largest[0]}.') |
||
print(f'The total number of colorful numbers is {csum}.') |
print(f'The total number of colorful numbers is {csum}.') |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Colorful numbers for 1:25, 26:50, 51:75, and 76:100: |
Colorful numbers for 1:25, 26:50, 51:75, and 76:100: |
||
Line 959: | Line 959: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>sub is-colorful (Int $n) { |
||
return True if 0 <= $n <= 9; |
return True if 0 <= $n <= 9; |
||
return False if $n.contains(0) || $n.contains(1) || $n < 0; |
return False if $n.contains(0) || $n.contains(1) || $n < 0; |
||
Line 990: | Line 990: | ||
} |
} |
||
say "\nTotal colorful numbers: $total";</ |
say "\nTotal colorful numbers: $total";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Colorful numbers less than 100: |
<pre>Colorful numbers less than 100: |
||
Line 1,021: | Line 1,021: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "./math" for Int, Nums |
||
import "./set" for Set |
import "./set" for Set |
||
import "./seq" for Lst |
import "./seq" for Lst |
||
Line 1,093: | Line 1,093: | ||
} |
} |
||
Fmt.print("\nTotal colorful numbers: $,d", Nums.sum(count))</ |
Fmt.print("\nTotal colorful numbers: $,d", Nums.sum(count))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,123: | Line 1,123: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func IPow(A, B); \A^B |
||
int A, B, T, I; |
int A, B, T, I; |
||
[T:= 1; |
[T:= 1; |
||
Line 1,194: | Line 1,194: | ||
IntOut(0, Total); |
IntOut(0, Total); |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |