Colorful numbers: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 36:
=={{header|C}}==
The <code>count_colorful</code> function is based on [[#Phix|Phix]].
<langsyntaxhighlight lang="c">#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
Line 126:
(end - start + 0.0) / CLOCKS_PER_SEC);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 158:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs grouping grouping.extras io kernel literals math
math.combinatorics math.ranges prettyprint project-euler.common
sequences sequences.extras sets ;
Line 187:
"Count of colorful numbers by number of digits:" print
8 [1..b] [ oom-count ] zip-with dup .
"Total: " write values sum .</langsyntaxhighlight>
{{out}}
<pre>
Line 219:
{{trans|Phix}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 338:
}
fmt.Printf("\nTotal colorful numbers: %s\n", rcu.Commatize(sum))
}</langsyntaxhighlight>
 
{{out}}
Line 369:
=={{header|Haskell}}==
{{incorrect|Haskell|10 is not a Colorful number; nor is 98765432: <nowiki>6×5×4 == 5×4×3×2 == 120</nowiki>}}
<langsyntaxhighlight lang="haskell">import Data.List ( nub )
import Data.List.Split ( divvy )
import Data.Char ( digitToInt )
Line 391:
 
solution2 :: Integer
solution2 = head $ filter isColourful [98765432, 98765431 ..]</langsyntaxhighlight>
{{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)
Line 398:
=={{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.
<langsyntaxhighlight lang="haskell">import Data.List (inits, nub, tails, unfoldr)
 
-- Non-empty subsequences containing only consecutive elements from the
Line 429:
let start = 98765432
largest = head $ dropWhile (not . isColorful) [start, start-1 ..]
putStrLn $ "Largest colorful number: " ++ show largest</langsyntaxhighlight>
{{out}}
<pre>
Line 439:
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j"> colorful=: {{(-:~.);<@(*/\)\. 10 #.inv y}}"0
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
Line 456:
7 14256
#C
57256</langsyntaxhighlight>
 
(Note that 0, here is a different order of magnitude than 1.)
Line 462:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">public class ColorfulNumbers {
private int count[] = new int[8];
private boolean used[] = new boolean[10];
Line 545:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 574:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">largest = 0
 
function iscolorful(n, base=10)
Line 610:
testcolorfuls()
</langsyntaxhighlight>{{out}}
<pre>
Colorful numbers for 1:25, 26:50, 51:75, and 76:100:
Line 630:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ColorfulNumberQ]
ColorfulNumberQ[n_Integer?NonNegative] := Module[{digs, parts},
If[n > 98765432,
Line 652:
Tally[IntegerDigits/*Length /@ cns] // Grid
 
Length[cns]</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8
Line 679:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 710:
my $total= 10;
map { is_colorful(join '', @$_) and $total++ } map { permutations $_ } combinations [2..9], $_ for 2..8;
say "Total colorful numbers: $total";</langsyntaxhighlight>
{{out}}
<pre>Colorful numbers less than 100:
Line 728:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/colourful.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 820:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">colorful_number(N) =>
N < 10 ;
(X = N.to_string,
Line 830:
 
distinct(L) =>
L.len == L.remove_dups.len.</langsyntaxhighlight>
 
'''All colorful numbers <= 100.'''
<langsyntaxhighlight Picatlang="picat">main =>
Colorful = [N : N in 0..100, colorful_number(N)],
Len = Colorful.len,
Line 840:
end,
nl,
println(len=Len)</langsyntaxhighlight>
 
{{out}}
Line 853:
 
'''Largest colorful number.'''
<langsyntaxhighlight Picatlang="picat">main =>
N = 98765431,
Found = false,
Line 862:
end,
N := N - 1
end.</langsyntaxhighlight>
 
{{out}}
Line 868:
 
'''Count of colorful numbers in each magnitude and of total colorful numbers.'''
<langsyntaxhighlight Picatlang="picat">main =>
TotalC = 0,
foreach(I in 1..8)
Line 887:
% For N=3: lb=123 and ub=987
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.</langsyntaxhighlight>
 
{{out}}
Line 901:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from math import prod
 
largest = [0]
Line 939:
print(f'The largest possible colorful number is {largest[0]}.')
print(f'The total number of colorful numbers is {csum}.')
</langsyntaxhighlight>{{out}}
<pre>
Colorful numbers for 1:25, 26:50, 51:75, and 76:100:
Line 959:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub is-colorful (Int $n) {
return True if 0 <= $n <= 9;
return False if $n.contains(0) || $n.contains(1) || $n < 0;
Line 990:
}
 
say "\nTotal colorful numbers: $total";</langsyntaxhighlight>
{{out}}
<pre>Colorful numbers less than 100:
Line 1,021:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./math" for Int, Nums
import "./set" for Set
import "./seq" for Lst
Line 1,093:
}
 
Fmt.print("\nTotal colorful numbers: $,d", Nums.sum(count))</langsyntaxhighlight>
 
{{out}}
Line 1,123:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IPow(A, B); \A^B
int A, B, T, I;
[T:= 1;
Line 1,194:
IntOut(0, Total);
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
10,327

edits