Powershell, 147 byte (versi 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)}
Catatan: Script menyelesaikan kasus uji terakhir kurang dari 3 menit pada notebook lokal saya. Lihat solusi "kinerja" di bawah ini.
Skrip uji yang kurang golf:
$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"
}
Keluaran:
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 byte (versi "Kinerja")
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)}
Catatan: Saya percaya persyaratan kinerja bertentangan dengan prinsip GodeGolf. Tetapi karena ada aturan Your program should solve any of the test cases below in less than a minute
, saya membuat dua perubahan untuk memenuhi aturan:
-split'(.)'-ne''
alih-alih kode pendek |% t*y
;
- hashtable untuk menguangkan string.
Setiap perubahan mengurangi setengah waktu evaluasi. Tolong jangan berpikir bahwa saya telah menggunakan semua fitur untuk meningkatkan kinerja. Hanya itu saja sudah cukup untuk memenuhi aturan.
Skrip uji yang kurang golf:
$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"
}
Keluaran:
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
Ç€=$
akan menjadi sedikit lebih cepat daripadaÇ€=Ç
, mengingat batasan waktu.