• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

perfect number problem

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends i am coding currently for the perfect number assignment. First let me explain the problem statement.

A perfect number is a number whose sum is formed by its factors and including 1 but not the number.

ie. 6 is a perfect number because 1+2+3=6; similarly i am trying to find whether the numbers from 1 to 10 are perfect or not.

The following is my listing.


I am not getting the output as desired.
This is my current output.
Perfect Number Perfect Makers
-------------- --------------
1
2 1+
3 1+1+
4 1+1+1+
5 1+1+1+1+
6 1+1+1+1+1+
7 1+1+1+1+1+1+
8 1+1+1+1+1+1+1+
9 1+1+1+1+1+1+1+1+
10 1+1+1+1+1+1+1+1+1+

Please help me complete this question...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few observations:
  • sum and factors never get "reset." Shouldn't these start over with each iteration of i?
  • You are adding j (a potential divisor) to sum before determining whether i%j is 0. Is that what you want?
  • What is the purpose of testing whether i/j == i?
  • I don't see any test for when the sum of the divisors equals i.


  • My suggestion is to carefully describe the logical process you want to follow in English first. Once this is clear, then write your code from that.
     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    'status' is a terrible name for a variable. status of WHAT?

    your code is kind of hard to follow. I think you need more methods, instead of doing it all in one.

    but working with what you have...

    your outer loop is apparently testing the numbers 1-10.

    your inner loop is testing every number from 1 to the current number you're testing (I think)

    i don't understand the sum=sum+j. I assume, although I can't tell, that sum is supposed to hold the sum of all factors. You don't know if j is a factor, so why are you adding it to the sum? for example, if i=5, you're sum would first add 1, then add 2, then add 3...

    why test if sum < i? what you need to do is find ALL all the factors, and then see if it equals i...

    i%j==0)||(i/j==i)

    if i/j == i, then i%j ==0 will also be true. I don't see the need for both of these tests...

    ================================

    I guess my real problem is I don't understand your logic. How are you trying to solve this? Tell us in words, not code. what EXACT steps should you take to solve this?

    The best way to code is to do the tiniest piece at time you can. You can start at the top or the bottom, but code as little as possible and be sure it works.

    For example, you could start by writing code that does nothing more than prints 1-10.

    Then write a method that you pass an int into, and have it say "i'm testing <whatever was passed in>"

    Then have that method call another method that finds the factors of the number passed in (maybe it returns an int [])

    Then write a method that can sum up all the values in an int []

    each step should be thoroughly tested before you THINK about writing the next one.

    that's my advice.
     
    kunagu varun
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wow what a great piece of advice friends.

    As you said the sum variable is must be placed inside the if() and not in the for...

    I will try to modularize the code this time and i will post back if i have problems still.

    Thanks a million for wonderful suggestion friends...
     
    kunagu varun
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i have a small doubt.



    in the above code,the break statement in the else part causes break to what?? either the if..else part or breaks out of the for loop..??
     
    kunagu varun
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i have modified my code as little bit friends. Please have a look at it.



    I will explain the flow of my code.
    First for loop is to process numbers from 2 to 10. Then prints whatever the value is in i. lets say i=6
    Inside this the second for loop is for this purpose. Iterate from 1 till the number before 6
    Either (6%1) or (6/1) satisfies and sum<6 satisfies then
    1.appending the factor (i.e 1) to the string
    2.appending the "+" symbol to the string
    3.storing the factor in the sum so that when sum has reached 6 then it wont enter this if segment.
    So as per my code first 1 enters and appended to string and added to sum then 2 and 3 also performs the same procedure and the sum value now is 6.
    Therefore the next value of j is 4 and for this value if condition is dis satisfied now and moves to the else block.
    here i am checking whether sum is 6 or not. Obviously our sum is 6 here so the status of the perfect number is set to true by default and it will not be modified by else block.
    Then it encounters the labeled break statement stop and exits the for inner for loop. Since the status is true,it must print the factors that make 6 as a perfect number. I think my logic is correct.
    But i get the following output now.
    Perfect Number Perfect Makers
    -------------- --------------
    2 1+
    3 Not a Perfect Number
    4 Not a Perfect Number
    5 Not a Perfect Number
    6 Not a Perfect Number
    7 Not a Perfect Number
    8 Not a Perfect Number
    9 Not a Perfect Number
    10 Not a Perfect Number

    I dont understand where i have went wrong
    Please look at the code once and reply to this..
     
    Ranch Hand
    Posts: 174
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wouldn't it be easier to check the sum of all positive divisors? You know it must be 2 * number for a perfect number, e.g. (1+2+3+6) = 12 -> 12 / 2 = 6.

    You just could loop from 1 to number and sum up all divisors, then check the sum:
     
    kunagu varun
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok thanks for replying. I will try it out and inform you back...
     
    fred rosenberger
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I tried to simplify things.

    I got rid of the code that loops from 2-10. Got rid of the break. Got rid of the test class. Basically, I stripped it down to the code that would test a number to see if it's 'perfect' or not.

    I hard-coded it to run for an input of '6'. It worked just fine.
    I hard-coded it to run for an input of '5'. It worked just fine.



    I really changed nothing in your logic. there was some weirdness with the sigma character posted in your example, but I think that's a quirk of the ranch.

    In any case, I think your logic works. this is what I meant by separating things out. ALL I have here is the code needed to test a number, and the bare minimum to test a single number. I can test the heck out of this, and be SURE it works. Once this is solid, I can then look at writing the code to test a bunch of numbers.>
     
    reply
      Bookmark Topic Watch Topic
    • New Topic