How to store these data in appropriate mannar?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moizpalitanawala
    New Member
    • Jul 2008
    • 14

    How to store these data in appropriate mannar?

    Hello Friends
    I am having a problem in finding the solution for this Question. Please help me.

    I am having a number say "n". In this case let us consider n=3.

    What i have to do is find first "n" multiples of "n".
    Store the answers somewhere.

    Find again the first "n" multiples of the answers.
    Store the another answers(N1) generated.

    Find again the first "n" multiples of the answers(N1) .
    Store the answers generated.

    Repeat the process "n" times.

    So the tree will be formed like this for "n"=3.

    ---------------1--------------------------------- 2 ----------------------------------------3
    1 ------------2----------3----//-----2----------4--------------6----///----3-----------6--------------9
    123-------2 4 6----3 6 9-///-2 4 6------4 8 12---- 6 12 18-//-3 6 9----6 12 18-----9 18 27

    The "----" and "////" are just show insted of spaces.

    Each variable should be stored in such a way. So that I can locate easily from which nodes it was generated.

    I dont want the program but just the logic of how to do that.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hm, sounds to me like you could do with a storage class. Something like
    Code:
    class Storage {
          int value;
          Collection<Storage> furtherValues;
          ...
    }
    Now, that is incomplete and you'd have to choose some type of collection and write setters and getters and so on, but the idea is, that you have a list (or other type of collection) of these Storage classes for the first line and each of them has a further collection with all values related to itself. I'll try to make a graphic:
    Code:
    1--------------------2--------------------3
    |                    |                    |
    1------2------3      2------4------6      3------6------9
    |      |      |      |      |      |      |      |      |
    1-2-3  2-4-6  3-6-9...
    I hope, you get the idea.

    Greetings,
    Nepomuk

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I don't think you have to store anything at all: if a node number equals 'i' then
      you immediately know its children: 1*i, 2*i ... n*i for your chosen value of 'n'.

      Finding the parents isn't much more difficult either: let p_1 p_2 ... p_m be the
      prime divisors of i. The procuct of them all needs to be divisable by either 2, 3,
      ... n otherwise 'i' can't be a node in the tree. Neither can there be a prime divisor
      p_i > n. If the condition is met then any value of [i-n, i] that can be evenly divided
      by n results in a possible parent for node i.

      kind regards,

      Jos

      edit: changed the clumsy text in the last paragraph.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by JosAH
        I don't think you have to store anything at all: ...
        Well, I guess it's some kind of homework and they should store the data - but of course, you're right, it's easy to calculate it again and again and again.

        Which way is the right one, only the OP can decide.

        Greetings,
        Nepomuk

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          You can store your data in a tree structure that is already inside the java API (no need to make your own class and methods to handle the data).
          just use:

          javax.swing.tre e.DefaultMutabl eTreeNode

          Although it's swing library, you don't need to know anything about swing or do anything with swing to use it. Just use it as internal structure for your data, and experience the power of the methods "pathFromAncest orEnumeration() " or "postorderEnume ration()" or "isNodeAnchesto r(anotherNode)" without writing a single line of code!

          Comment

          Working...