Display all Prime Factors from user input

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.



# int ----> list
# returns factor list of a num
def factor(num):
factors =
for i in range(num):
if not (i == 0 or i == 1):
if num % i == 0:
factors.append(i)
return factors

# int ----> bool
# returns true if num is prime
def prime(num):
for i in range(num):
if not(i == 0 or i == 1):
if num % i == 0:
return False
return True

# main
# prints the factors of a number which are prime
def primeFactorization():
num = int(input("Enter the number: "))
factors = factor(num)
prime_factors =

for item in factors:
if prime(item):
prime_factors.append(item)
print(prime_factors)

primeFactorization()






share|improve this question



























    up vote
    2
    down vote

    favorite












    The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.



    # int ----> list
    # returns factor list of a num
    def factor(num):
    factors =
    for i in range(num):
    if not (i == 0 or i == 1):
    if num % i == 0:
    factors.append(i)
    return factors

    # int ----> bool
    # returns true if num is prime
    def prime(num):
    for i in range(num):
    if not(i == 0 or i == 1):
    if num % i == 0:
    return False
    return True

    # main
    # prints the factors of a number which are prime
    def primeFactorization():
    num = int(input("Enter the number: "))
    factors = factor(num)
    prime_factors =

    for item in factors:
    if prime(item):
    prime_factors.append(item)
    print(prime_factors)

    primeFactorization()






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.



      # int ----> list
      # returns factor list of a num
      def factor(num):
      factors =
      for i in range(num):
      if not (i == 0 or i == 1):
      if num % i == 0:
      factors.append(i)
      return factors

      # int ----> bool
      # returns true if num is prime
      def prime(num):
      for i in range(num):
      if not(i == 0 or i == 1):
      if num % i == 0:
      return False
      return True

      # main
      # prints the factors of a number which are prime
      def primeFactorization():
      num = int(input("Enter the number: "))
      factors = factor(num)
      prime_factors =

      for item in factors:
      if prime(item):
      prime_factors.append(item)
      print(prime_factors)

      primeFactorization()






      share|improve this question













      The following program is written in python. Is there a more efficient way to write this program? Suggestions and criticism are welcome. You can also comment about my variable naming practices and code readability.



      # int ----> list
      # returns factor list of a num
      def factor(num):
      factors =
      for i in range(num):
      if not (i == 0 or i == 1):
      if num % i == 0:
      factors.append(i)
      return factors

      # int ----> bool
      # returns true if num is prime
      def prime(num):
      for i in range(num):
      if not(i == 0 or i == 1):
      if num % i == 0:
      return False
      return True

      # main
      # prints the factors of a number which are prime
      def primeFactorization():
      num = int(input("Enter the number: "))
      factors = factor(num)
      prime_factors =

      for item in factors:
      if prime(item):
      prime_factors.append(item)
      print(prime_factors)

      primeFactorization()








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 10 at 20:08









      Sam Onela

      5,77461543




      5,77461543









      asked May 10 at 19:50









      Aditya Garg

      576




      576




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          For both of your functions prime and factor you have something like:



          for i in range(num):
          if not (i == 0 or i == 1):
          if num % i == 0:
          factors.append(i)


          when you could shorten by using range(start,stop):



          for i in range(2,num):
          if num % i == 0:
          factors.append(i)





          share|improve this answer





















          • Oh I did not notice that! Thanks for pointing out.
            – Aditya Garg
            May 10 at 20:01










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194138%2fdisplay-all-prime-factors-from-user-input%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          For both of your functions prime and factor you have something like:



          for i in range(num):
          if not (i == 0 or i == 1):
          if num % i == 0:
          factors.append(i)


          when you could shorten by using range(start,stop):



          for i in range(2,num):
          if num % i == 0:
          factors.append(i)





          share|improve this answer





















          • Oh I did not notice that! Thanks for pointing out.
            – Aditya Garg
            May 10 at 20:01














          up vote
          2
          down vote



          accepted










          For both of your functions prime and factor you have something like:



          for i in range(num):
          if not (i == 0 or i == 1):
          if num % i == 0:
          factors.append(i)


          when you could shorten by using range(start,stop):



          for i in range(2,num):
          if num % i == 0:
          factors.append(i)





          share|improve this answer





















          • Oh I did not notice that! Thanks for pointing out.
            – Aditya Garg
            May 10 at 20:01












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          For both of your functions prime and factor you have something like:



          for i in range(num):
          if not (i == 0 or i == 1):
          if num % i == 0:
          factors.append(i)


          when you could shorten by using range(start,stop):



          for i in range(2,num):
          if num % i == 0:
          factors.append(i)





          share|improve this answer













          For both of your functions prime and factor you have something like:



          for i in range(num):
          if not (i == 0 or i == 1):
          if num % i == 0:
          factors.append(i)


          when you could shorten by using range(start,stop):



          for i in range(2,num):
          if num % i == 0:
          factors.append(i)






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 10 at 19:58









          depperm

          30319




          30319











          • Oh I did not notice that! Thanks for pointing out.
            – Aditya Garg
            May 10 at 20:01
















          • Oh I did not notice that! Thanks for pointing out.
            – Aditya Garg
            May 10 at 20:01















          Oh I did not notice that! Thanks for pointing out.
          – Aditya Garg
          May 10 at 20:01




          Oh I did not notice that! Thanks for pointing out.
          – Aditya Garg
          May 10 at 20:01












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194138%2fdisplay-all-prime-factors-from-user-input%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?