क्या मैं पूर्ण (संख्या) हूं?


26

यह मेरी पहली चुनौती है!

पृष्ठभूमि

परफेक्ट नंबर एक पॉजिटिव पूर्णांक है, जो अपने को छोड़कर, इसके सभी विभाजकों के योग के बराबर है।
तो 6एकदम सही संख्या है, क्योंकि 1 + 2 + 3 = 6
दूसरी तरफ 12नहीं है, क्योंकि 1 + 2 + 3 + 4 + 6 = 16 != 12

कार्य

आपका कार्य सरल है, एक प्रोग्राम लिखें, जो दिए nगए इन संदेशों में से एक को प्रिंट करेगा :

मैं एक परफेक्ट नंबर d1 + d2 + ... + dm = s == n
हूं , क्योंकि मैं एक परफेक्ट नंबर नहीं हूं, क्योंकिd1 + d2 + ... + dm = s [<>] n

जहां
d1, ... dmके nअलावा सभी विभाजक हैं n
sसभी भाजक d1, ..., dm(फिर, बिना n) का योग है ।
[<>]या तो <(यदि है s < n) या >(यदि s > n)।

उदाहरण

के लिए nकिया जा रहा है 6"मैं, एक आदर्श संख्या हूँ क्योंकि 1 + 2 + 3 = 6 == 6":
के लिए nकिया जा रहा है 12: "मुझे नहीं एक आदर्श संख्या हूँ, क्योंकि 1 + 2 + 3 + 4 + 6 = 16> 12"
के लिए nकिया जा रहा है 13: "मैं एक पूर्ण संख्या नहीं हूं, क्योंकि 1 = 1 <13"

नियम

  • nआपकी भाषा के मानक से बड़ा नहीं है int
  • आप nमानक इनपुट से, कमांड लाइन के तर्क से या किसी फ़ाइल से पढ़ सकते हैं ।
  • आउटपुट संदेश को मानक आउटपुट पर प्रिंट करना होता है और कोई भी अतिरिक्त वर्ण आउटपुट में नहीं दिखाई देता है (इसमें अनुगामी व्हाट्सएप या न्यूलाइन हो सकता है)
  • आप किसी भी बिल्ट-इन या लाइब्रेरी फ़ंक्शंस का उपयोग नहीं कर सकते हैं जो आपके लिए कार्य (या इसका मुख्य भाग) को हल करेगा। नहीं GetDivisors()या ऐसा कुछ।
  • अन्य सभी मानक खामियां लागू होती हैं।

विजेता

यह बाइट्स जीत में इतना छोटा कोड है !


@orlp यह नहीं है, मैंने चुनौती को संपादित किया, इसके लिए धन्यवाद।
ज़ेरेगेस 15'15

7
आप क्यों =और ==उसी समीकरण में उपयोग करते हैं ? इसका कोई अर्थ नही बन रहा है। यह d1 + d2 + ... + dm = s = nIMO होना चाहिए ।
orlp

क्या आप कुछ उदाहरण इनपुट और आउटपुट दे सकते हैं, उदाहरण के लिए 6 और 12 इनपुट के साथ?
जरगब

14
@Zereges यह बकवास है। कुछ भी सौंपा नहीं जा रहा है। केवल तुलना की।
orlp

1
@orlp यह इरादा है।
ज़ेरेगेस 15'15

जवाबों:


4

पायथ, 81 बाइट्स

jd[+WK-QsJf!%QTStQ"I am"" not""a perfect number, because"j" + "J\=sJ@c3"==<>"._KQ

इसे ऑनलाइन आज़माएँ: प्रदर्शन या टेस्ट सूट

स्पष्टीकरण:

                                 implicit: Q = input number
               StQ               the range of numbers [1, 2, ..., Q-1]
          f                      filter for numbers T, which satisfy:
           !%QT                     Q mod T != 0
         J                       save this list of divisors in J
      -QsJ                       difference between Q and sum of J
     K                           save the difference in K

jd[                              put all of the following items in a list
                                 and print them joined by spaces: 
                  "I am"           * "I am"
   +WK                  " not"       + "not" if K != 0
"a perfect number, because"        * "a perfect ..."
j" + "J                            * the divisors J joined by " + "
       \=                          * "="
         sJ                        * sum of J
            c3"==<>"               * split the string "==<>" in 3 pieces:
                                        ["==", "<", ">"]
           @        ._K              and take the (sign of K)th one (modulo 3)
                       Q           * Q

9

जावा, 255 270 बाइट्स (अभी भी बेस 17 में एफएफ)

class C{public static void main(String[]a){int i=new Integer(a[0]),k=0,l=0;a[0]=" ";for(;++k<i;)if(i%k<1){l+=k;a[0]+=k+" ";}}System.out.print("I am "+(l==i?"":"not ")+"a perfect number, because "+a[0].trim().replace(" "," + ")+" = "+l+(l==i?" == ":l<i?" < ":" > ")+i);}}

और अधिक पठनीय संस्करण:

class C {
    public static void main(String[] a) {
        int i = new Integer(a[0]), k = 0, l = 0;
        a[0] = " ";
        for(; ++k<i ;){
            if (i % k == 0) {
                l += k;
                a[0] += k + " ";
            }
        }
        System.out.print("I am " + (l == i ? "" : "not ") + "a perfect number, because " + a[0].trim().replace(" "," + ") + " = " + l + (l == i ? " == " : l < i ? " < " : " > ") + i);
    }
}

पहले विषम संख्याओं के लिए काम नहीं किया था, इसलिए मुझे कुछ चीजों को मोड़ना पड़ा। कम से कम मैं बाइट की गिनती के साथ फिर से भाग्यशाली हो गया। :)


l == मैं 255 से अधिक काम करूंगा?
dwana

मुझे पता है कि अगर आप अपनी बाइट की गिनती को बर्बाद करते हैं, लेकिन आप एक [0] की अंतिम तीन (चार) घटनाओं को बदलकर एक चरित्र को बचा सकते हैं एक 'स्ट्रिंग b' के साथ और उनके स्थान पर 'b' का उपयोग करें
क्रेग

6

आर, 158 163 157 157 153 143 बाइट्स

मुझे लगता है कि अभी भी गोल्फ के लिए कमरा।
संपादित करें: बदला if(b<n)'<'else if(b>n)'>'else'=='के साथ c('<'[b<n],'>'[b>n],'=='[b==n])paste(...)एक साथ बदल दिया है rbind(...)[-1]। कुछ और बाइट्स के लिए @plannapus धन्यवाद।

n=scan();a=2:n-1;b=sum(w<-a[!n%%a]);cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)

Ungolfed

n<-scan()             # get number from stdin
w<-which(!n%%1:(n-1)) # build vector of divisors
b=sum(w)              # sum divisors
cat('I am',           # output to STDOUT with a space separator
    'not'[b!=n],      # include not if b!=n
    'a perfect number, because',
    rbind('+',w)[-1], # create a matrix with the top row as '+', remove the first element of the vector
    '=',
    b,                # the summed value
    c(                # creates a vector that contains only the required symbol and ==
        '<'[b<n],     # include < if b<n
        '>'[b>n],     # include > if b>n
        '=='
    )[1],             # take the first element 
    n                 # the original number
)

परीक्षण चालन

> n=scan();b=sum(w<-which(!n%%1:(n-1)));cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
1: 6
2: 
Read 1 item
I am a perfect number, because 1 + 2 + 3 = 6 == 6
> n=scan();b=sum(w<-which(!n%%1:(n-1)));cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
1: 12
2: 
Read 1 item
I am not a perfect number, because 1 + 2 + 3 + 4 + 6 = 16 > 12
> n=scan();b=sum(w<-which(!n%%1:(n-1)));cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
1: 13
2: 
Read 1 item
I am not a perfect number, because 1 = 1 < 13
> 

+भाजक के बीच हस्ताक्षर होना चाहिए ।
ज़ेरेगेस 15'15

@Zereges मैंने अभी देखा है और जल्द ही ठीक हो जाएगा
मिकी

शानदार rbindचाल के लिए +1 ! यदि आप 2:n-1एक चर को असाइन करते हैं, तो आप 2 अतिरिक्त बाइट्स बचा सकते हैं , कहते हैं a: which(!n%%1:(n-1)) इस प्रकार बन जाता है a[!n%%a]। (तब पूरा कोड n=scan();a=2:n-1;b=sum(w<-a[!n%%a]);cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n))
प्लैनपस

@plannapus धन्यवाद, मैं वास्तव में खुद से प्रसन्न था।
मिकट

5

पायथन 2, 183 173 170 बाइट्स

b=input();c=[i for i in range(1,b)if b%i<1];d=sum(c);print'I am %sa perfect number because %s = %d %s %d'%('not '*(d!=b),' + '.join(map(str,c)),d,'=<>='[cmp(b,d)%3::3],b)

उदाहरण:

$ python perfect_number.py <<< 6
I am a perfect number because 1 + 2 + 3 = 6 == 6
$ python perfect_number.py <<< 12
I am not a perfect number because 1 + 2 + 3 + 4 + 6 = 16 > 12
$ python perfect_number.py <<< 13
I am not a perfect number because 1 = 1 < 13
$ python perfect_number.py <<< 100
I am not a perfect number because 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 > 100
$ python perfect_number.py <<< 8128
I am a perfect number because 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064 = 8128 == 8128

13 बाइट बचाने के लिए xnor का धन्यवाद !


4
'=<>'[cmp(b,d)]- क्रांति में शामिल हो!
orlp

उत्कृष्ट धन्यवाद! ओह, रुको ... :)
सेलेओ

1
@ कैले मैं एक समान समाधान के साथ आया था। आप के b%i<1लिए लिख सकते हैं b%i==0। इसके लिए ['not ',''][int(d==b)], आपको इसकी आवश्यकता नहीं है int, क्योंकि पायथन स्वचालित रूप से परिवर्तित हो जाएगा। इसके अलावा, आप स्ट्रिंग mulitplication का उपयोग कर सकते हैं "not "*(d!=b)
3

@xnor सुझाव के लिए धन्यवाद!
सेलेओ

1
@Celeo आप orlp के सुझाव को काम करने के लिए समायोजित कर सकते हैं "=<>="[cmp(b,d)%3::3]
xnor

4

जूलिया, 161 157 बाइट्स

n=int(ARGS[1])
d=filter(i->n%i<1,1:n-1)
s=sum(d)
print("I am ",s!=n?"not ":"","a perfect number, because ",join(d," + ")," = $s ",s<n?"<":s>n?">":"=="," $n")

Ungolfed:

# Read n as the first command line argument
n = int(ARGS[1])

# Get the divisors of n and their sum
d = filter(i -> n % i == 0, 1:n-1)
s = sum(d)

# Print to STDOUT
print("I am ",
      s != n ? "not " : "",
      "a perfect number, because ",
      join(d, " + "),
      " = $s ",
      s < n ? "<" : s > n ? ">" : "==",
      " $n")

4

सीजेएम, 90 बाइट्स

"I am"rd:R{R\%!},:D:+R-g:Cz" not"*" a perfect number, because "D'+*'=+D:++'=C+_'=a&+a+R+S*

तुलना के लिए, =83 बाइट्स में एक एकल मुद्रण प्राप्त किया जा सकता है।

CJam दुभाषिया में इसे ऑनलाइन आज़माएं ।

यह काम किस प्रकार करता है

"I am"  e# Push that string.
rd:R    e# Read a Double from STDIN and save it in R.
{       e# Filter; for each I in [0 ... R-1]:
  R\%!  e# Push the logical NOT of (R % I).
},      e# Keep the elements such that R % I == 0.
:D      e# Save the array of divisors in D.
:+R-g   e# Add the divisors, subtract R and compute the sign of the difference.
:Cz     e# Save the sign in C and apply absolute value.
"not "* e# Repeat the string "not " that many times.

" a perfect number, because "

D'+*    e# Join the divisors, separating by plus signs.
'=+D:++ e# Append a '=' and the sum of the divisors.
'=C+    e# Add the sign to '=', pushing '<', '=' or '>'.
_'=a&   e# Intersect a copy with ['='].
+a+     e# Concatenate, wrap in array and concatenate.
        e# This appends "<", "==" or ">".
R+      e# Append the input number.
S*      e# Join, separating by spaces.

2

पर्ल, 148 बाइट्स

$a=<>;$_=join' + ',grep{$a%$_==0}1..$a-1;$s=eval;print"I am ".($s==$a?'':'not ')."a perfect number because $_ = $s ".(('==','>','<')[$s<=>$a])." $a"

लाइन ब्रेक के साथ:

$a=<>;
$_=join' + ',grep{$a%$_==0}1..$a-1;
$s=eval;
print"I am ".($s==$a?'':'not ')."a perfect number because $_ = $s ".(('==','>','<')[$s<=>$a])." $a"

मैं इस पर एक प्रहार किया है और आप के आसपास बाहरी कोष्ठक को हटाने के द्वारा 10 बाइट्स बचा सकता है 'not 'और '==','>','<'बयानों और से स्विच .करने के लिए ,(कुछ भी नहीं के बाद से जब जोड़ा जाता है printएक सूची ing)। इसके अलावा अपने असाइनमेंट को पहली बार पारेंस में ले जाने से वे एक जोड़े को बचाते हैं, और यदि आप तर्क को थोड़ा बदलते हैं grep$a%_<1,1..($a=<>)-1और $a!=($s=eval)&&'not 'आपको कुछ और शेव करना चाहिए! आशा है कि सभी समझ में आता है!
डोम हेस्टिंग्स

2

लुआ, 244 231 बाइट्स

golfed:

n=io.read("*n")d={}s="1"t=1 for i=2,n-1 do if n%i==0 then table.insert(d,i)s=s.." + "..i t=t+i end end print(("I am%s a perfect number, because %s = %s"):format(t==n and""or" not", s, t..(t==n and" == "or(t>n and" > "or" < "))..n))

Ungolfed:

n=io.read("*n")
divisors={}
sequence="1"
sum=1
for i=2,n-1 do
    if n%i==0 then 
        table.insert(divisors,i)
        sequence=sequence.." + "..i
        sum=sum+i
    end
end

print(("I am%s a perfect number, because %s = %s"):format(sum==n and""or" not", sequence, sum..(sum==n and" == "or(sum>n and" > "or" < "))..n))

2

जावास्क्रिप्ट (ईएस 6), 146

टेम्पलेट स्ट्रिंग्स का उपयोग करना, यह फ़ायरफ़ॉक्स और नवीनतम क्रोम में काम करता है।

for(n=prompt(),o=t=i=1;++i<n;)n%i||(t+=i,o+=' + '+i)
alert(`I am ${t-n?'not ':''}a perfect number because ${o} = ${t} ${t<n?'<':t>n?'>':'=='} `+n)


2

रूबी, 174 160 155 136 134 128 122 बाइट्स

n=6;a=[*1...n].reject{|t|n%t>0};b=a.inject(:+)<=>n;print"I am#{" not"*b.abs} a perfect number, because ",a*?+,"<=>"[b+1],n

एक और 6 बाइट्स बचाए :)

रूबी में गोल्फ के लिए टिप्स के लिए धन्यवाद


प्रिंट कमांड अभी भी मुझे परेशान करता है .. और मुझे इफ-स्टेटमेंट टर्नरी को छोटा करने का तरीका निकालने की ज़रूरत है? एक और क्लॉज़ की आवश्यकता है जो मैं प्रदान नहीं कर सकता और केवल एक कॉल प्रति मामले में स्वीकार करता हूं
यूरी काजाकोव

केवल एक प्रिंट स्टेटमेंट बचा है :)
यूरी काजाकोव

1

सी #, 252 बाइट्स

class A{static void Main(string[]a){int i=int.Parse(a[0]);var j=Enumerable.Range(1,i-1).Where(o=>i%o==0);int k=j.Sum();Console.Write("I am "+(i!=k?"not ":"")+"a perfect number, because "+string.Join(" + ",j)+" = "+k+(k>i?" > ":k<i?" < ":" == ")+i);}}

1

हासियम , 285 बाइट्स

अस्वीकरण: कमांड लाइन आर्ग के मुद्दों के कारण हासियम के केवल नवीनतम संस्करण के साथ काम करता है।

func main(){n=Convert.toNumber(args[0]);s=1;l="1";foreach(x in range(2,n-3)){if(n%x==0){l+=" + "+x;s+=x;}}if(s==n)println("I am a perfect number, because "+l+" = "+s+" == "+s);else {print("I am not a perfect number, because "+l+" = "+s);if(s>n)println(" > "+n);else println(" < "+n);}}

अधिक पठनीय संस्करण:

func main() {
    n = Convert.toNumber(args[0]);
    s = 1;
    l = "1";
    foreach(x in range(2, n - 3)) {
            if (n % x== 0) {
                    l += " + " + x;
                    s += x;
            }
    }
    if (s == n)
            println("I am a perfect number, because " + l + " = " + s + " == " + s);
    else {
            print("I am not a perfect number, because " + l + " = " + s);
            if (s > n)
                    println(" > " + n);
            else
                    println(" < " + n);
    }

}


1
1. मैं अपने कमांड लाइन तर्क को पढ़ने के लिए हासियम को समझाने के लिए प्रतीत नहीं कर सकता। अगर मैं निष्पादित करता हूं mono src/Hassium/bin/Debug/Hassium.exe t.hs 6, तो यह कहता है System.ArgumentException: The file 6 does not exist.। 2. यह इस संस्करण के साथ काम नहीं करता है , जो इस चुनौती को पोस्ट करने से पहले सबसे अंतिम प्रतिबद्ध है। कृपया अपने उत्तर में एक अस्वीकरण जोड़ें जो बताता है कि आपका सबमिशन गैर-प्रतिस्पर्धी है।
डेनिस

मैंने इसे विंडोज पर आज़माया (MVS2015 का उपयोग करके बनाया गया) और इसे एक ही त्रुटि मिली।
ज़ेरेगेस

यह एक मुद्दा है जिसे 15 मिनट पहले शाब्दिक रूप से अद्यतन किया गया था। हमोस को क्लोन करें और फिर से संकलित करें। मुझे खेद है कि मैं ठीक उसी समस्या में भाग गया।
याकूब मिसिरियन

1
यह नवीनतम संस्करण के साथ ठीक काम करता है। अब अगर आप सिर्फ डिस्क्लेमर जोड़ सकते हैं, तो मुझे अपने डाउनवोट को हटाकर खुशी होगी। (वैसे, आप मुझे @Dennisअपनी टिप्पणी में जोड़कर पिंग कर सकते हैं । अन्यथा, मुझे आपके उत्तर की सूचना नहीं है।)
डेनिस

@ डेनिस मैंने इसे जोड़ा। आपकी अधिसूचना के लिए धन्यवाद :)
याकूब मिसिरियन

1

MATLAB, 238

कभी भी सभी भाषाओं में सबसे छोटा नहीं है, लेकिन यहाँ MATLAB के साथ मेरा प्रयास है:

n=input('');x=1:n-1;f=x(~rem(n,x));s=sum(f);a='not ';b=strjoin(strtrim(cellstr(num2str(f')))',' + ');if(s>n) c=' > ';elseif(s<n) c=' < ';else c=' == ';a='';end;disp(['I am ' a 'a perfect number, because ' b ' = ' num2str(s) c num2str(n)])

और यह थोड़ा अधिक पठनीय रूप में है:

n=input();      %Read in the number using the input() function
x=1:n-1;        %All integers from 1 to n-1
f=x(~rem(n,x)); %Determine which of those numbers are divisors
s=sum(f);       %Sum all the divisors
a='not ';       %We start by assuming it is not perfect (to save some bytes)
b=strjoin(strtrim(cellstr(num2str(f')))',' + '); %Also convert the list of divisors into a string 
                                                 %where they are all separated by ' + ' signs.
%Next check if the number is >, < or == to the sum of its divisors
if(s>n)  
    c=' > ';    %If greater than, we add a ' > ' to the output string
elseif(s<n) 
    c=' < ';    %If less than, we add a ' < ' to the output string
else
    c=' == ';   %If equal, we add a ' == ' to the output string
    a='';       %If it is equal, then it is a perfect number, so clear the 'not' string
end

%Finally concatenate the output string and display the result
disp(['I am ' a 'a perfect number, because ' b ' = ' num2str(s) c num2str(n)])

मैं एक फ़ंक्शन का उपयोग न करके 2 और बाइट्स सहेजने में कामयाब रहा हूं। इसके बजाय आप कोड की लाइन चलाते हैं और यह इनपुट के रूप में नंबर का अनुरोध करता है। एक बार चलाने के बाद यह आखिर में आउटपुट प्रदर्शित करता है।


1

पर्ल 6 , 138 बाइट्स

$_=get;
my$c=$_ <=>my$s=[+] my@d=grep $_%%*,^$_;
say "I am {
    'not 'x?$c
  }a perfect number, because {
    join ' + ',@d
  } = $s {
    «> == <»[1+$c]
  } $_"

(गणना नई कथानक, और संकेत की उपेक्षा करती है, क्योंकि उनकी आवश्यकता नहीं है)

@dभाजक धारण करने वाली सरणी है।
$sभाजक का योग रखता है।
$cइनपुट के बीच तुलना का मूल्य है, और भाजक का योग है।
(प्रभावी रूप $cसे एक है -1, 0, 1, लेकिन वास्तव में से एक है Order::Less, Order::Sameया Order::More)

में 'not 'x?$c, ?$cइस मामले में प्रभावी रूप से के रूप में ही है abs $c, और xस्ट्रिंग पुनरावृत्ति ऑपरेटर है।

«> == <»के लिए छोटा है ( '>', '==', '<' )
चूंकि $cइनमें से एक है -1,0,1, इसलिए हमें इसे एक सूची में अनुक्रमित करने के लिए उपयोग करने में सक्षम होने के लिए इसे स्थानांतरित करना होगा।

तकनीकी रूप से यह संख्या 2⁶⁴ से ऊपर की संख्या के लिए काम करेगा, लेकिन 2¹⁶ से ऊपर की संख्या के लिए समय की एक विषम राशि लेता है।


0

पायथ, 84 बाइट्स

jd+,+"I am"*.aK._-QsJf!%QTtUQ" not""a perfect number, because"+.iJm\+tJ[\=sJ@"=<>"KQ

अमान्य उत्तर, क्योंकि मैं लागू करने से इनकार करता हूं =और ==उसी समीकरण में।


2
"एक ही समीकरण में" = और == को लागू करने से इनकार करने के लिए +1।
theonlygusti

0

रूबी, 164 बाइट्स

->i{t=(1...i).select{|j|i%j==0};s=t.inject &:+;r=['==','>','<'][s<=>i];puts "I am #{'not ' if r!='=='}a perfect number, because #{t.join(' + ')} = #{s} #{r} #{i}"}

परीक्षा

irb(main):185:0> ->i{t=(1...i).select{|j|i%j==0};s=t.inject &:+;r=['==','>','<'][s<=>i];puts "I am #{'not ' if r!='=='}a perfect number, because #{t.join(' + ')} = #{s} #{r} #{i}"}.call 6
I am a perfect number, because 1 + 2 + 3 = 6 == 6

irb(main):186:0> ->i{t=(1...i).select{|j|i%j==0};s=t.inject &:+;r=['==','>','<'][s<=>i];puts "I am #{'not ' if r!='=='}a perfect number, because #{t.join(' + ')} = #{s} #{r} #{i}"}.call 12
I am not a perfect number, because 1 + 2 + 3 + 4 + 6 = 16 > 12

irb(main):187:0> ->i{t=(1...i).select{|j|i%j==0};s=t.inject &:+;r=['==','>','<'][s<=>i];puts "I am #{'not ' if r!='=='}a perfect number, because #{t.join(' + ')} = #{s} #{r} #{i}"}.call 13
I am not a perfect number, because 1 = 1 < 13

0

एमएसीएस लिस्प, 302 बाइट्स

(defun p(n)(let((l(remove-if-not'(lambda(x)(=(% n x)0))(number-sequence 1(- n 1)))))(setf s(apply'+ l))(format"I am%s a perfect number, because %s%s = %s %s %s"(if(= s n)""" not")(car l)(apply#'concat(mapcar'(lambda(x)(concat" + "(number-to-string x)))(cdr l)))s(if(= sum n)"=="(if(> sum n)">""<"))n)))

Ungolfed संस्करण:

(defun perfect (n)
  (let ((l (remove-if-not '(lambda (x) (= (% n x) 0))
              (number-sequence 1 (- n 1)))))
    (setf sum (apply '+ l))
    (format "I am%s a perfect number, because %s%s = %s %s %s" (if (= sum n)"" " not") (car l)
        (apply #'concat (mapcar '(lambda (x) (concat " + " (number-to-string x))) (cdr l)))
        sum (if(= sum n)
            "=="
          (if(> sum n)
              ">"
            "<"))
        n)))

0

पॉवरशेल, 164 बाइट्स

$a=$args[0]
$b=(1..($a-1)|?{!($a%$_)})-join" + "
$c=iex $b
$d=$a.compareto($c)
"I am $("not "*!!$d)a perfect number, because $b = $c $(("==","<",">")[$d]) $a"

कुछ आम और इतने सामान्य PoSh ट्रिक्स नहीं;

  • योग बनाएं, फिर इसका मूल्यांकन आइवीएक्स से करें
  • Gt, lt, eq array को इंडेक्स करने के लिए तुलना करें
  • !! $ d $ d = 1 या -1 के लिए सही == 1 का मूल्यांकन करेगा, और $ = = 0 के लिए गलत == 0 होगा

0

जाग, १५०

n=$0{for(p=i=s=n>1;++i<n;)for(;n%i<1;p+=i++)s=s" + "i;printf"I am%s a perfect number, because "s" = "p" %s "n RS,(k=p==n)?_:" not",k?"==":p<n?"<":">"}

इनपुट के लिए इसे सही बनाने पर कुछ बाइट्स बर्बाद 1। मुझे उम्मीद नहीं है कि उम्मीद है।

n=$0{
    for(p=i=s=n>1;++i<n;)
        for(;n%i<1;p+=i++)s=s" + "i;
    printf "I am%s a perfect number, because "s" = "p" %s "n RS,
           (k=p==n)?_:" not",k?"==":p<n?"<":">"
}

0

05AB1E , 58 बाइट्स

„I€ÜIѨ©OIÊi'€–}“€…íÀ‚³,ƒ«“®vy'+}\'=®ODI.S"==><"211S£sèIðý

इसे ऑनलाइन आज़माएं या सभी परीक्षण मामलों को सत्यापित करें

स्पष्टीकरण:

Iۆ              # Push dictionary string "I am"
IѨ               # Push the divisors of the input-integer, with itself removed
   ©              # Store it in the register (without popping)
    O             # Get the sum of these divisors
     IÊi   }      # If it's not equal to the input-integer:
        '€–      '#  Push dictionary string "not"
“€…íÀ‚³,ƒ«“       # Push dictionary string "a perfect number, because"
®v   }            # Loop `y` over the divisors:
  y'+            '#  Push the divisor `y`, and the string "+" to the stack
      \           # Discard the final "+"
       '=        '# And push the string "="
®O                # Get the sum of the divisors again
  D               # Duplicate it
I.S               # Compare it to the input-integer (-1 if smaller; 0 if equal; 1 if larger)
   "==><"         # Push string "==><"
         211S£    # Split into parts of size [2,1,1]: ["==",">","<"]
              sè  # Index into it (where the -1 will wrap around to the last item)
I                 # Push the input-integer again
ðý                # Join everything on the stack by spaces
                  # (and output the result implicitly)

मेरा यह 05AB1E टिप देखें (अनुभाग कैसे शब्दकोश उपयोग कैसे करें? ) को समझने के लिए क्यों „I€Üहै "I am", '€–है "not", और “€…íÀ‚³,ƒ«“है "a perfect number, because"

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.