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
Ç€=$
때보 다 약간 빠를 것이라고 생각합니다Ç€=Ç
.