\ $n\$-numéros parfaits
Un entier positif \$x\$est un \$n\$-numéro parfait si \$\sigma(x) = nx\$, où \$\sigma(x)\$est la fonction de somme des diviseurs. Par exemple, \$120\$est un \$3\$-nombre parfait car la somme de ses diviseurs est \$360\$:
$$360 = 3\times120 = 1+2+3+4+5+6+8+10+12+15+20+24+30+40+60+120$$
et
$$926073336514623897600 = 6\times154345556085770649600 = 1+2+3+4+5+6+7+8+9+10+11+12+\dots+51448518695256883200+77172778042885324800+154345556085770649600$$
alors \$154345556085770649600\$est un \$6\$-numéro parfait.
Vous devez prendre un entier \$x\$comme entrée et sortie une valeur \$n\$, tel que \$x\$est un \$n\$-numéro parfait. Si ce n'est pas le cas \$n\$existe, vous pouvez afficher toute valeur cohérente qui n'est pas un entier positif. Vous ne recevrez jamais une entrée en dehors des limites de votre langue, mais votre algorithme doit fonctionner pour une valeur arbitraire \$x\$.
C'est du code-golf donc le code le plus court en octets l'emporte.
Mini défi: battre 5 octets dans Jelly
Cas de test
x -> n
1 -> 1
2 -> 0
3 -> 0
4 -> 0
5 -> 0
6 -> 2
28 -> 2
120 -> 3
496 -> 2
500 -> 0
672 -> 3
30240 -> 4
154345556085770649600 -> 6
Réponses
Husk , 4 octets
¦¹ΣḊ
Essayez-le en ligne!
Le dernier scénario de test expire.
Explication
¦¹ΣḊ Input is a number x.
Ḋ List of divisors.
Σ Sum.
¦ Division if divisible, 0 if not
¹ by x.
¦ est généralement juste un test de divisibilité, mais ici sa valeur de retour est utile.
Gelée , 5 octets
R×iÆs
Essayez-le en ligne!
Explication:
- Explanation (sample for input 6)
R - Range ([1, 2, 3, 4, 5, 6])
× - Multiply by input ([6, 12, 18, 24, 30, 36])
Æs - Divisor sum (12)
i - Index of divisor sum in list, else 0 (2)
Brachylog , 6 octets
f+;?/ℕ
Essayez-le en ligne!
Comment ça fonctionne
f+;?/ℕ
f+ the sum of the factors
;?/ℕ divided by the input
ℕ is a natural number
Version alternative, je pense que c'est plus cool, mais plus long:
f+~×[?,.]∧
f+ the sum of the factors
~× unifies with the multiplication of
[?,.] the input and the output
∧ return the output
05AB1E ,
7
6 octets
Ý*IÑOk
Essayez-le en ligne!
Explication:
Ý*IÑOk>
Ý 0-Index inclusive range of input (6 -> [1, 2, 3, 4, 5, 6])
* Multiply by input ([6, 12, 18, 24, 30, 36])
IÑO Get input -> divisors -> sum (6 -> [1, 2, 3, 6] -> 12)
k 0-Index of divisor-sum in array or -1 if not found. ([6, >12<, 18, 24, 30, 36] -> 1)
Je viens d'utiliser la méthode de Sisyphe. Cela pourrait probablement être réduit au golf ou même rendu plus efficace, mais je n'ai pas les connaissances 05AB1E pour le faire. Je pensais juste que j'essaierais de passer le temps.
-1 octet grâce à ovs
C (gcc) , 47 octets
s,i;f(x){for(i=s=x;--i;)s+=x%i?0:i;s/=s%x*s+x;}
Essayez-le en ligne!
Renvoie nou 0.
APL (Dyalog Unicode) , 16 15 13 octets
Merci à Bubbler pour avoir signalé que je peux changer le format de sortie pour économiser quelques octets
⍸×∘⍳⍨=1⊥∘∪⊢∨⍳
Essayez-le en ligne!
Affiche une liste de singleton nquand nexiste, et un tableau vide dans le cas contraire. Recherche l'index de ( ⍸) où la somme ( 1⊥) des diviseurs ( ∪⊢∨⍳) est égale à ( =) un multiple de l'entrée ( ×∘⍳⍨). J'utilise ⍸et =au lieu de simplement ⍳pour trouver l'index car il renvoie une liste vide lorsque l'élément n'est pas là plutôt que la longueur de la liste.
Espace blanc , 153 octets
[S S S N
_Push_0][S N
S _Duplicate_0][T N
T T _Read_STDIN_as_integer][T T T _Retrieve_input][S N
S _Duplicate_input][S N
S _Duplicate_input][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T S S T _Subtract][S N
S _Duplicate][N
T S S N
_If_0_Jump_to_Label_REACHED_ZERO][S T S S T S N
_Copy_0-based_2nd_input][S T S S T N
_Copy_0-based_1st_integer][T S T T _Modulo][N
T S T N
_If_0_Jump_to_Label_ADD_TO_SUM][N
S N
N
_Jump_to_Label_LOOP][N
S S T N
_Create_Label_ADD_TO_SUM][S N
T _Swap_top_two][S T S S T N
_Copy_0-based_1st_integer][T S S S _Add_top_two][S N
T _Swap_top_two][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_REACHED_ZERO][S N
N
_Discard_top][S N
S _Duplicate_top][S T S S T S N
_Copy_0-based_2nd_input][T S T T _Modulo][N
T S S S N
_If_0_Jump_to_Label_DIVISIBLE][S S S N
_Push_0][N
S N
S T
_Jump_to_Label_OUTPUT][N
S S S S N
_Create_Label_DIVISIBLE][S N
T _Swap_top_two][T S T S _Integer_divide_top_two][N
S S S T N
_Create_Label_OUTPUT][T N
S T _Output_as_integer]
Lettres S(espace), T(tabulation) et N(nouvelle ligne) ajoutées en surbrillance uniquement.
[..._some_action]ajouté à titre d'explication uniquement.
Essayez-le en ligne (avec des espaces bruts, des onglets et des nouvelles lignes uniquement).
Explication en pseudo-code:
Integer input = STDIN as input
Integer sum = input
Integer i = input
Start LOOP:
i = i - 1
If(i == 0):
Jump to Label REACHED_ZERO
If(input % i == 0):
sum = sum + i
Go to next iteration of LOOP
Label REACHED_ZERO:
Integer output
If(sum % input == 0):
output = sum integer-divided by input
Else:
output = 0
Print output as integer to STDOUT
Exemple d'exécution: input = 6
Command Explanation Stack Heap STDIN STDOUT STDERR
SSSN Push 0 [0]
SNS Duplicate top (0) [0,0]
TNTT Read STDIN as integer [0] {0:6} 6
TTT Retrieve at address (0) [6] {0:6}
SNS Duplicate top (6) [6,6] {0:6}
SNS Duplicate top (6) [6,6,6] {0:6}
NSSN Create Label LOOP [6,6,6] {0:6}
SSSTN Push 1 [6,6,6,1] {0:6}
TSST Subtract top two (6-1) [6,6,5] {0:6}
SNS Duplicate top (5) [6,6,5,5] {0:6}
NTSSN If 0: Jump to Label REACHED_ZERO [6,6,5] {0:6}
STSSTSN Copy 0-based 2nd (6) [6,6,5,6] {0:6}
STSSTN Copy 0-based 1st (5) [6,6,5,6,5] {0:6}
TSTT Modulo top two (6%5) [6,6,5,1] {0:6}
NTSTN If 0: Jump to Label ADD_TO_SUM [6,6,5] {0:6}
NSNN Jump to Label LOOP [6,6,5] {0:6}
SSSTN Push 1 [6,6,5,1] {0:6}
TSST Subtract top two (5-1) [6,6,4] {0:6}
SNS Duplicate top (4) [6,6,4,4] {0:6}
NTSSN If 0: Jump to Label REACHED_ZERO [6,6,4] {0:6}
STSSTSN Copy 0-based 2nd (6) [6,6,4,6] {0:6}
STSSTN Copy 0-based 1st (4) [6,6,4,6,4] {0:6}
TSTT Modulo top two (6%4) [6,6,4,2] {0:6}
NTSTN If 0: Jump to Label ADD_TO_SUM [6,6,4] {0:6}
NSNN Jump to Label LOOP [6,6,4] {0:6}
SSSTN Push 1 [6,6,4,1] {0:6}
TSST Subtract top two (4-1) [6,6,3] {0:6}
SNS Duplicate top (3) [6,6,3,3] {0:6}
NTSSN If 0: Jump to Label REACHED_ZERO [6,6,3] {0:6}
STSSTSN Copy 0-based 2nd (6) [6,6,3,6] {0:6}
STSSTN Copy 0-based 1st (3) [6,6,3,6,3] {0:6}
TSTT Modulo top two (6%3) [6,6,3,0] {0:6}
NTSTN If 0: Jump to Label ADD_TO_SUM [6,6,3] {0:6}
NSSTN Create Label ADD_TO_SUM [6,6,3] {0:6}
SNT Swap top two [6,3,6] {0:6}
STSSTN Copy 0-based 1st (3) [6,3,6,3] {0:6}
TSSS Add top two (6+3) [6,3,9] {0:6}
SNT Swap top two [6,9,3] {0:6}
NSNN Jump to Label LOOP [6,9,3] {0:6}
SSSTN Push 1 [6,9,3,1] {0:6}
TSST Subtract top two (3-1) [6,9,2] {0:6}
SNS Duplicate top (2) [6,9,2,2] {0:6}
NTSSN If 0: Jump to Label REACHED_ZERO [6,9,2] {0:6}
STSSTSN Copy 0-based 2nd (6) [6,9,2,6] {0:6}
STSSTN Copy 0-based 1st (5) [6,9,2,6,2] {0:6}
TSTT Modulo top two (6%5) [6,9,2,0] {0:6}
NTSTN If 0: Jump to Label ADD_TO_SUM [6,9,2] {0:6}
SNT Swap top two [6,2,9] {0:6}
STSSTN Copy 0-based 1st (2) [6,2,9,2] {0:6}
TSSS Add top two (9+2) [6,2,11] {0:6}
SNT Swap top two [6,11,2] {0:6}
NSNN Jump to Label LOOP [6,11,2] {0:6}
SSSTN Push 1 [6,11,2,1] {0:6}
TSST Subtract top two (2-1) [6,11,1] {0:6}
SNS Duplicate top (1) [6,11,1,1] {0:6}
NTSSN If 0: Jump to Label REACHED_ZERO [6,11,1] {0:6}
STSSTSN Copy 0-based 2nd (6) [6,11,1,6] {0:6}
STSSTN Copy 0-based 1st (1) [6,11,1,6,1] {0:6}
TSTT Modulo top two (6%1) [6,11,1,0] {0:6}
NTSTN If 0: Jump to Label ADD_TO_SUM [6,11,1] {0:6}
SNT Swap top two [6,1,11] {0:6}
STSSTN Copy 0-based 1st (1) [6,1,11,1] {0:6}
TSSS Add top two (11+1) [6,1,12] {0:6}
SNT Swap top two [6,12,1] {0:6}
NSNN Jump to Label LOOP [6,12,1] {0:6}
SSSTN Push 1 [6,12,1,1] {0:6}
TSST Subtract top two (1-1) [6,12,0] {0:6}
SNS Duplicate top (1) [6,12,0,0] {0:6}
NTSSN If 0: Jump to Label REACHED_ZERO [6,12,0] {0:6}
NSSSN Create Label REACHED_ZERO [6,12,0] {0:6}
SNN Discard top (0) [6,12] {0:6}
SNS Duplicate top (12) [6,12,12] {0:6}
STSSTSN Copy 0-based 2nd (6) [6,12,12,6] {0:6}
TSTT Modulo top two (12%6) [6,12,0] {0:6}
NTSSSN If 0: Jump to Label DIVISIBLE [6,12] {0:6}
NSSSSN Create Label DIVISIBLE [6,12] {0:6}
SNT Swap top two [12,6] {0:6}
TSTS Integer-divide top two (12/6) [2] {0:6}
NSSSTN Create Label OUTPUT [2] {0:6}
TNST Output top as integer (2) [] {0:6} 2
error
S'arrête avec une erreur après l'impression du résultat, car aucune sortie n'est définie.
Pyth , 15 octets
&!%Jsf!%QTSQQ/J
Essayez-le en ligne!
Explication
&!%Jsf!%QTSQQ/J
J # set J to
s # sum of
f SQ # filtering the range [1, input] with
!%QT # lambda T: not (input % T) (divisibility test)
# implicit print the
& # short-circuiting and of
!%J Q # not (J % input)
/J # and J / input
JavaScript (ES6), 41 octets
Renvoie 0 s'il n'y a pas de solution.
x=>(g=k=>x=k&&k*!(x%k)/x+g(k-1))(x)%1?0:x
Essayez-le en ligne!
Gelée , 6 octets
Æs0:%?
Un lien monadique acceptant un entier positif qui donne un entier non négatif.
Essayez-le en ligne! Ou voir la suite de tests .
Comment?
Æs0:%? - Link: x
Æs - divisor sum
? - if...
% - ...condition: has a remainder when divided
0 - ...then: zero
: - ...else: integeger divide
APL (Dyalog Unicode) , 19 18 octets
⊢(÷⍨×0=|)1⊥∘⍸0=⍳|⊢
Essayez-le en ligne!
Conversion en train par Jo King. (- 3 octets)
-1 octet de plus de Jo King après avoir changé la condition de vérification.
Réponse plus ancienne, 22 octets
{(⊢×⌊=⊢)⍵÷⍨+/⍸0=⍵|⍨⍳⍵}
Explication
{(⊢×⌊=⊢)⍵÷⍨+/⍸0=⍵|⍨⍳⍵} ⍵ → input
⍳⍵ range 1-⍵
⍵|⍨ mod ⍵
0= check which ones are divisors
⍸ get the indices (factors)
+/ sum the factors
⍵÷⍨ divide by ⍵
(⊢×⌊=⊢) Inner tacit fn:
⌊=⊢ Floor equals right? (integer test, returns 0 or 1)
⊢× times right
Haskell , 51 46 octets
a!b=0^mod a b*div a b
f n=sum(map(n!)[1..n])!n
Essayez-le en ligne!
Wolfram Language (Mathematica) , 30 octets
Tr@Divisors@#/#/._Rational->0&
Essayez-le en ligne!
-6 octets de @att
Charbon , 23 20 octets
NθI⌕E⊕θ×θιΣΦ⊕θ∧ι¬﹪θι
Essayez-le en ligne! Le lien est vers la version verbeuse du code. Port de l'algorithme de @ Sisyphus mais utilisant le commentaire de @ ovs pour gérer l'indexation 0. Sorties -1pour non-existence. Explication:
Nθ Input `x` as a number
θ `x`
⊕ Incremented
Φ Filter over implicit range
ι Current index
∧ Logical AND
θ `x`
﹪ Modulo
ι Current index
¬ Logical NOT
Σ Take the sum
θ `x`
⊕ Incremented
E Map over implicit range
θ `x`
× Multiplied by
ι Current index
⌕ Find the index
I Cast to string
Implicitly print
Malheureusement pour Charcoal, la somme de []n'est pas nulle, ce qui signifie que je ne peux pas enregistrer un octet en supprimant les deux incréments de xet en incrémentant le résultat à la place.
Solution précédente de 23 octets:
Nθ≔ΣΦ⊕θ∧ι¬﹪θιη¿¬﹪ηθI÷ηθ
Essayez-le en ligne! Le lien est vers la version verbeuse du code. Explication:
Nθ
Entrée x.
≔ΣΦ⊕θ∧ι¬﹪θιη
Créez une liste à partir de 1..x, filtrez les nombres qui ne se divisent pas xet prenez la somme.
¿¬﹪ηθI÷ηθ
Si xdivise la somme, imprimez le quotient.
R , 42 41 39 octets
Edit: -1 octet (et, inspiré par cela, -2 octets de plus) grâce à Robin Ryder
function(x)(d=sum(1:x*!x%%1:x))/x*!d%%x
Essayez-le en ligne!
Commenté:
perfect_n=
function(x)
(d= # d is the divisor sum, calculated as...
sum( # sum of...
1:x* # the values of 1..x that have...
! # zero values for...
x%%1:x) # x MOD 1..x
)
)/x # output d/x...
*!d%%x # but only if it's an integer
# (so d MOD x == 0)
Scala, 54 53 octets
x=>{val s=1 to x filter(x%_<1)sum;s/x*(1-(s%x).sign)}
Essayez-le dans Scastie
Somme tous les diviseurs de x1 à x, inclus. Si cette somme est divisible par x, elle renvoie celle divisée par x, sinon elle renvoie 0.
Rétine , 51 octets
.+
*
|""Lw`^(.+)(?=\1*$) ^ $-1;
L$`^(.+);(\1)+$
$#2
Essayez-le en ligne! Link inclut des cas de test moins lents. Explication:
.+
*
Convertissez l'entrée en unaire.
|""Lw`^(.+)(?=\1*$)
Dressez la liste de tous les facteurs sans les délimiter, les additionnant ainsi.
^
$-1;
Récupérez la valeur unaire d'origine.
L$`^(.+);(\1)+$ $#2
Comptez combien de fois il divise la somme. (Ou ne rien afficher si ce n'est pas le cas.)
Octave , 36 34 octets
@(x)~mod(s=~mod(x,r=1:x)*r',x)*s/x
Fonction anonyme qui prend en entrée un nombre à virgule flottante ou un nombre entier. Le dernier scénario de test échoue en raison de limitations de mémoire.
Essayez-le en ligne! Ou vérifiez les cas de test .
Explication
@(x)~mod(s=~mod(x,r=1:x)*r',x)*s/x
@(x) % anonymous function with input x
1:x % row vector [1 2 ... x]
r= % call that r
mod(x, ) % x modulo [1 2 ... x]. Gives a row vector
~ % negate each element. Gives 1 for divisors
r' % column vector [1; 2; ... ; x]
* % matrix-multiply. Gives the sum of divisors
s= % call that s
mod( ,x) % sum of divisors modulo x
~ % negate. Gives 1 if x divides sum of divisors
s/x % sum of divisors divided by x
* % multiply
Python 3.8 (pré-version) , 62 octets
lambda x:(a:=sum(x/i*(x%i<1)for i in range(1,x+1)))%x<1and a/x
Essayez-le en ligne!
MathGolf , 7 octets
─Σk‼÷/*
Essayez-le en ligne.
Explication:
─ # Get the divisors of the (implicit) input-integer
Σ # Sum those divisors
k # Push the input-integer again
‼ # Apply the following two commands separately to the stack:
÷ # Check if the divisor-sum is divisible by the input (1 if truthy; 0 if falsey)
/ # Integer-divide the divisor-sum by the input
* # Multiply the two together
# (after which the entire stack joined together is output implicitly as result)
Rockstar , 141 135 131 octets
Ne produit rien si aucun nn'existe.
listen to N
X's0
T's0
while N-X
let X be+1
let D be N/X
turn up D
let T be+D is N/X and X
let D be T/N
turn up D
if D is T/N
say D
Essayez-le ici (le code devra être collé)
Icône , 67 octets
procedure f(n)
s:=0
n%(i:=1to n)=0&s+:=i&\z
return(0=s%n&s/n)|0
end
Essayez-le en ligne!
Japt -æ , 7 octets
Sorties undefinedsi aucun nn'est trouvé.
*N¶Îâ x
Essayez-le
Facteur , 84 octets
: f ( n -- n ) dup [1,b] [ dupd mod 0 = ] filter sum swap /mod 0 > [ drop 0 ] when ;
Essayez-le en ligne!
Prologue, 117 octets
s(X,D,S):-D<1,!,S is 0;E is D-1,(0 is X mod D,!,s(X,E,T),S is T+D;s(X,E,S)).
f(X,N):-s(X,X,S),0 is S mod X,N is S//X.
Essayez-le en ligne! (Veuillez ne pas le modifier directement, cela changerait aussi ma version)
Si quelqu'un pouvait comprendre pourquoi cette version plus courte (96 octets) ne fonctionne pas, je serais vraiment reconnaissant.
s(X,D,S):-D<1,!,S is 0;E is D-1,(0 is X mod D,!,s(X,E,T),S is T+D;s(X,E,S)).
f(X,N):-s(X,X,N*X).
Version avec débogage d'impression
GolfScript , 22 octets
~:x),{.x\%!*+}*.x%!*x/
Essayez-le en ligne!
~:x # Store the input in x
), # Make an array from 0 to x
{ }* # For each number in the array, execute this block
. # Copy current number
x\%! # The copy becomes 1 if it is a divisor of x and 0 if it isn't
*+ # Multiply and add
. # Copy the sum of the divisors
x%! # The copy becomes 1 if it is a divisor of x and 0 if it isn't
* # Multiply
x/ # Divide by x