Python coding, nested loops









up vote
0
down vote

favorite












Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.



**If I want to count the numbers between 16 and 100( 5,6,7,8,9 =makes 5)and write code in terms of with i and j, my code would be as follows but something goes wrong. I want to get the result,5 at last. how can I correct it?



 k=16
m=100
i=0
j=0
q1=0
q2=0
while j**2 <m:
q2=q2+1
while i**2 <k:
q1=q1+1
i=i+1
j=j+1
print(q2-q1)









share|improve this question

























    up vote
    0
    down vote

    favorite












    Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.



    **If I want to count the numbers between 16 and 100( 5,6,7,8,9 =makes 5)and write code in terms of with i and j, my code would be as follows but something goes wrong. I want to get the result,5 at last. how can I correct it?



     k=16
    m=100
    i=0
    j=0
    q1=0
    q2=0
    while j**2 <m:
    q2=q2+1
    while i**2 <k:
    q1=q1+1
    i=i+1
    j=j+1
    print(q2-q1)









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.



      **If I want to count the numbers between 16 and 100( 5,6,7,8,9 =makes 5)and write code in terms of with i and j, my code would be as follows but something goes wrong. I want to get the result,5 at last. how can I correct it?



       k=16
      m=100
      i=0
      j=0
      q1=0
      q2=0
      while j**2 <m:
      q2=q2+1
      while i**2 <k:
      q1=q1+1
      i=i+1
      j=j+1
      print(q2-q1)









      share|improve this question













      Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.



      **If I want to count the numbers between 16 and 100( 5,6,7,8,9 =makes 5)and write code in terms of with i and j, my code would be as follows but something goes wrong. I want to get the result,5 at last. how can I correct it?



       k=16
      m=100
      i=0
      j=0
      q1=0
      q2=0
      while j**2 <m:
      q2=q2+1
      while i**2 <k:
      q1=q1+1
      i=i+1
      j=j+1
      print(q2-q1)






      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 9:49









      Gokce Ezeroglu

      62




      62






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          Your probably don't want to loop for this. If k and m are very far apart it will take a long time.



          Given k < m, you want to compute how many integers l such that k < l^2 < m. The smallest possible such integer is floor( sqrt(k) +1 ) and the largest possible such integer is ceil(sqrt(m)-1). The number of such integers is:



          import math

          def sq_between(k,m):
          return math.ceil(m**0.5-1) - math.floor(k**0.5+1) +1


          This allows for



          sq_between(16,100)


          yielding:



          5





          share|improve this answer






















          • This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
            – quant
            Nov 11 at 10:24











          • the code defines sq_between, thats what the "def" does.
            – Christian Sloper
            Nov 11 at 10:30

















          up vote
          0
          down vote













          Here is another version of your function that seems to do to what you ask for.



          k = 16
          m = 100
          perfect_squares =
          for i in range(m):
          if i**2 < k:
          continue
          if i**2 > m:
          break
          perfect_squares.append(i**2)
          print(perfect_squares)





          share|improve this answer



























            up vote
            0
            down vote













            You code is mixing up everything in the second while loop. If you explain a bit further what you are trying to do there, I will probably be able to explain why your idea is not working.



            I would change your code as follows in order to make it work:



            k = 10
            m = 40

            i = 0
            q = 0
            while i ** 2 < m:
            if i ** 2 > k:
            print(i)
            q += 1
            i += 1

            print (q)


            By utilizing the fact that each square number can get expressed via square = sum from i = 1 to n (2 * i + 1) there is an easy way of speedup the above algorithm - but the algorithm will become much longer then ...






            share|improve this answer






















              Your Answer






              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: "1"
              ;
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247520%2fpython-coding-nested-loops%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              Your probably don't want to loop for this. If k and m are very far apart it will take a long time.



              Given k < m, you want to compute how many integers l such that k < l^2 < m. The smallest possible such integer is floor( sqrt(k) +1 ) and the largest possible such integer is ceil(sqrt(m)-1). The number of such integers is:



              import math

              def sq_between(k,m):
              return math.ceil(m**0.5-1) - math.floor(k**0.5+1) +1


              This allows for



              sq_between(16,100)


              yielding:



              5





              share|improve this answer






















              • This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
                – quant
                Nov 11 at 10:24











              • the code defines sq_between, thats what the "def" does.
                – Christian Sloper
                Nov 11 at 10:30














              up vote
              1
              down vote













              Your probably don't want to loop for this. If k and m are very far apart it will take a long time.



              Given k < m, you want to compute how many integers l such that k < l^2 < m. The smallest possible such integer is floor( sqrt(k) +1 ) and the largest possible such integer is ceil(sqrt(m)-1). The number of such integers is:



              import math

              def sq_between(k,m):
              return math.ceil(m**0.5-1) - math.floor(k**0.5+1) +1


              This allows for



              sq_between(16,100)


              yielding:



              5





              share|improve this answer






















              • This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
                – quant
                Nov 11 at 10:24











              • the code defines sq_between, thats what the "def" does.
                – Christian Sloper
                Nov 11 at 10:30












              up vote
              1
              down vote










              up vote
              1
              down vote









              Your probably don't want to loop for this. If k and m are very far apart it will take a long time.



              Given k < m, you want to compute how many integers l such that k < l^2 < m. The smallest possible such integer is floor( sqrt(k) +1 ) and the largest possible such integer is ceil(sqrt(m)-1). The number of such integers is:



              import math

              def sq_between(k,m):
              return math.ceil(m**0.5-1) - math.floor(k**0.5+1) +1


              This allows for



              sq_between(16,100)


              yielding:



              5





              share|improve this answer














              Your probably don't want to loop for this. If k and m are very far apart it will take a long time.



              Given k < m, you want to compute how many integers l such that k < l^2 < m. The smallest possible such integer is floor( sqrt(k) +1 ) and the largest possible such integer is ceil(sqrt(m)-1). The number of such integers is:



              import math

              def sq_between(k,m):
              return math.ceil(m**0.5-1) - math.floor(k**0.5+1) +1


              This allows for



              sq_between(16,100)


              yielding:



              5






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 11 at 10:39

























              answered Nov 11 at 10:22









              Christian Sloper

              1,059213




              1,059213











              • This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
                – quant
                Nov 11 at 10:24











              • the code defines sq_between, thats what the "def" does.
                – Christian Sloper
                Nov 11 at 10:30
















              • This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
                – quant
                Nov 11 at 10:24











              • the code defines sq_between, thats what the "def" does.
                – Christian Sloper
                Nov 11 at 10:30















              This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
              – quant
              Nov 11 at 10:24





              This results in "NameError: name 'sq_between' is not defined" for me ... Same for "math.sq_between(0, 100)" ... Tried with both python 2 and pyhton 3 ...
              – quant
              Nov 11 at 10:24













              the code defines sq_between, thats what the "def" does.
              – Christian Sloper
              Nov 11 at 10:30




              the code defines sq_between, thats what the "def" does.
              – Christian Sloper
              Nov 11 at 10:30












              up vote
              0
              down vote













              Here is another version of your function that seems to do to what you ask for.



              k = 16
              m = 100
              perfect_squares =
              for i in range(m):
              if i**2 < k:
              continue
              if i**2 > m:
              break
              perfect_squares.append(i**2)
              print(perfect_squares)





              share|improve this answer
























                up vote
                0
                down vote













                Here is another version of your function that seems to do to what you ask for.



                k = 16
                m = 100
                perfect_squares =
                for i in range(m):
                if i**2 < k:
                continue
                if i**2 > m:
                break
                perfect_squares.append(i**2)
                print(perfect_squares)





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Here is another version of your function that seems to do to what you ask for.



                  k = 16
                  m = 100
                  perfect_squares =
                  for i in range(m):
                  if i**2 < k:
                  continue
                  if i**2 > m:
                  break
                  perfect_squares.append(i**2)
                  print(perfect_squares)





                  share|improve this answer












                  Here is another version of your function that seems to do to what you ask for.



                  k = 16
                  m = 100
                  perfect_squares =
                  for i in range(m):
                  if i**2 < k:
                  continue
                  if i**2 > m:
                  break
                  perfect_squares.append(i**2)
                  print(perfect_squares)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 10:26









                  ostcar

                  763




                  763




















                      up vote
                      0
                      down vote













                      You code is mixing up everything in the second while loop. If you explain a bit further what you are trying to do there, I will probably be able to explain why your idea is not working.



                      I would change your code as follows in order to make it work:



                      k = 10
                      m = 40

                      i = 0
                      q = 0
                      while i ** 2 < m:
                      if i ** 2 > k:
                      print(i)
                      q += 1
                      i += 1

                      print (q)


                      By utilizing the fact that each square number can get expressed via square = sum from i = 1 to n (2 * i + 1) there is an easy way of speedup the above algorithm - but the algorithm will become much longer then ...






                      share|improve this answer


























                        up vote
                        0
                        down vote













                        You code is mixing up everything in the second while loop. If you explain a bit further what you are trying to do there, I will probably be able to explain why your idea is not working.



                        I would change your code as follows in order to make it work:



                        k = 10
                        m = 40

                        i = 0
                        q = 0
                        while i ** 2 < m:
                        if i ** 2 > k:
                        print(i)
                        q += 1
                        i += 1

                        print (q)


                        By utilizing the fact that each square number can get expressed via square = sum from i = 1 to n (2 * i + 1) there is an easy way of speedup the above algorithm - but the algorithm will become much longer then ...






                        share|improve this answer
























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You code is mixing up everything in the second while loop. If you explain a bit further what you are trying to do there, I will probably be able to explain why your idea is not working.



                          I would change your code as follows in order to make it work:



                          k = 10
                          m = 40

                          i = 0
                          q = 0
                          while i ** 2 < m:
                          if i ** 2 > k:
                          print(i)
                          q += 1
                          i += 1

                          print (q)


                          By utilizing the fact that each square number can get expressed via square = sum from i = 1 to n (2 * i + 1) there is an easy way of speedup the above algorithm - but the algorithm will become much longer then ...






                          share|improve this answer














                          You code is mixing up everything in the second while loop. If you explain a bit further what you are trying to do there, I will probably be able to explain why your idea is not working.



                          I would change your code as follows in order to make it work:



                          k = 10
                          m = 40

                          i = 0
                          q = 0
                          while i ** 2 < m:
                          if i ** 2 > k:
                          print(i)
                          q += 1
                          i += 1

                          print (q)


                          By utilizing the fact that each square number can get expressed via square = sum from i = 1 to n (2 * i + 1) there is an easy way of speedup the above algorithm - but the algorithm will become much longer then ...







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 11 at 11:24

























                          answered Nov 11 at 10:18









                          quant

                          1,55911526




                          1,55911526



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247520%2fpython-coding-nested-loops%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線