Powershell,147个字节(CodeGolf版本)
param($n)filter d{-join($(for($i=2;$_-ge$i*$i){if($_%$i){$i++}else{"$i"
$_/=$i}}if($_-1){"$_"})|% t*y|sort -d)}2..($s=$n|d)|?{$_-$n-and$s-eq($_|d)}
注意:该脚本在我的本地笔记本上不到3分钟即可解决最后的测试用例。请参阅下面的“性能”解决方案。
少打高尔夫的测试脚本:
$g = {
param($n)
filter d{ # in the filter, Powershell automatically declares the parameter as $_
-join($( # this function returns a string with all digits of all prime divisors in descending order
for($i=2;$_-ge$i*$i){ # find all prime divisors
if($_%$i){
$i++
}else{
"$i" # push a divisor to a pipe as a string
$_/=$i
}
}
if($_-1){
"$_" # push a last divisor to pipe if it is not 1
}
)|% t*y|sort -d) # t*y is a shortcut to toCharArray method. It's very slow.
}
2..($s=$n|d)|?{ # for each number from 2 to number with all digits of all prime divisors in descending order
$_-$n-and$s-eq($_|d) # leave only those who have the 'all digits of all prime divisors in descending order' are the same
}
}
@(
,(2 ,'')
,(4 ,'')
,(6 ,23)
,(8 ,'')
,(15 ,53)
,(16 ,'')
,(23 ,6)
,(42 ,74, 146, 161)
,(107 ,701)
,(117 ,279, 939, 993, 3313, 3331)
,(126 ,222, 438, 483, 674, 746, 851, 1466, 1631, 1679)
,(204 ,364,548,692,762,782,852,868,1268,1626,2474,2654,2921,2951,3266,3446,3791,4274,4742,5426,5462,6233,6434,6542,7037,8561,14426,14642,15491,15833,22547)
) | % {
$n,$expected = $_
$sw = Measure-Command {
$result = &$g $n
}
$equals=$false-notin(($result|%{$_-in$expected})+($expected|?{$_-is[int]}|%{$_-in$result}))
"$sw : $equals : $n ---> $result"
}
输出:
00:00:00.0346911 : True : 2 --->
00:00:00.0662627 : True : 4 --->
00:00:00.1164648 : True : 6 ---> 23
00:00:00.6376735 : True : 8 --->
00:00:00.1591527 : True : 15 ---> 53
00:00:03.8886378 : True : 16 --->
00:00:00.0441986 : True : 23 ---> 6
00:00:01.1316642 : True : 42 ---> 74 146 161
00:00:01.0393848 : True : 107 ---> 701
00:00:05.2977238 : True : 117 ---> 279 939 993 3313 3331
00:00:12.1244363 : True : 126 ---> 222 438 483 674 746 851 1466 1631 1679
00:02:50.1292786 : True : 204 ---> 364 548 692 762 782 852 868 1268 1626 2474 2654 2921 2951 3266 3446 3791 4274 4742 5426 5462 6233 6434 6542 7037 8561 14426 14642 15491 15833 22547
Powershell,215字节(“性能”版本)
param($n)$p=@{}
filter d{$k=$_*($_-le3e3)
($p.$k=-join($(for($i=2;!$p.$_-and$_-ge$i*$i){if($_%$i){$i++}else{"$i"
$_/=$i}}if($_-1){($p.$_,"$_")[!$p.$_]})-split'(.)'-ne''|sort -d))}2..($s=$n|d)|?{$_-$n-and$s-eq($_|d)}
注意:我认为性能要求与GodeGolf原则相抵触。但是既然有规则Your program should solve any of the test cases below in less than a minute
,我做了两处更改以满足该规则:
-split'(.)'-ne''
代替短代码|% t*y
;
- 用于兑现字符串的哈希表。
每次更改都会使评估时间减少一半。请不要以为我已经使用了所有功能来提高性能。那些足以满足规则。
少打高尔夫的测试脚本:
$g = {
param($n)
$p=@{} # hashtable for 'all digits of all prime divisors in descending order'
filter d{ # this function returns a string with all digits of all prime divisors in descending order
$k=$_*($_-le3e3) # hashtable key: a large hashtable is not effective, therefore a key for numbers great then 3000 is 0
# and string '-le3e3' funny
($p.$k=-join($( # store the value to hashtable
for($i=2;!$p.$_-and$_-ge$i*$i){
if($_%$i){$i++}else{"$i";$_/=$i}
}
if($_-1){
($p.$_,"$_")[!$p.$_] # get a string with 'all digits of all prime divisors in descending order' from hashtable if it found
}
)-split'(.)'-ne''|sort -d)) # split each digit. The "-split'(.)-ne''" code is faster then '|% t*y' but longer.
}
2..($s=$n|d)|?{ # for each number from 2 to number with all digits of all prime divisors in descending order
$_-$n-and$s-eq($_|d) # leave only those who have the 'all digits of all prime divisors in descending order' are the same
}
}
@(
,(2 ,'')
,(4 ,'')
,(6 ,23)
,(8 ,'')
,(15 ,53)
,(16 ,'')
,(23 ,6)
,(42 ,74, 146, 161)
,(107 ,701)
,(117 ,279, 939, 993, 3313, 3331)
,(126 ,222, 438, 483, 674, 746, 851, 1466, 1631, 1679)
,(204 ,364,548,692,762,782,852,868,1268,1626,2474,2654,2921,2951,3266,3446,3791,4274,4742,5426,5462,6233,6434,6542,7037,8561,14426,14642,15491,15833,22547)
) | % {
$n,$expected = $_
$sw = Measure-Command {
$result = &$g $n
}
$equals=$false-notin(($result|%{$_-in$expected})+($expected|?{$_-is[int]}|%{$_-in$result}))
"$sw : $equals : $n ---> $result"
}
输出:
00:00:00.0183237 : True : 2 --->
00:00:00.0058198 : True : 4 --->
00:00:00.0181185 : True : 6 ---> 23
00:00:00.4389282 : True : 8 --->
00:00:00.0132624 : True : 15 ---> 53
00:00:04.4952714 : True : 16 --->
00:00:00.0128230 : True : 23 ---> 6
00:00:01.4112716 : True : 42 ---> 74 146 161
00:00:01.3676701 : True : 107 ---> 701
00:00:07.1192912 : True : 117 ---> 279 939 993 3313 3331
00:00:07.6578543 : True : 126 ---> 222 438 483 674 746 851 1466 1631 1679
00:00:50.5501853 : True : 204 ---> 364 548 692 762 782 852 868 1268 1626 2474 2654 2921 2951 3266 3446 3791 4274 4742 5426 5462 6233 6434 6542 7037 8561 14426 14642 15491 15833 22547
Ç€=$
会快一些Ç€=Ç
。