This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to split a number into two multipliyers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to split a number into two multipliyers" Watch "how to split a number into two multipliyers" New topic
Author

how to split a number into two multipliyers

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i want to split 32 into 8 and 4
i want to split 64 into 8 and 8
i want to split 48 into 6 and 8
its main two multipliyers

i dont know how to do it??
[ March 13, 2008: Message edited by: donaldth smithts ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Google is your friend.

Try this Wikipedia link.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Something to think about:

Should 12 be split into 3 * 4? Or 2 * 6? Or what about 48 - does it split to 6 * 8? 3 * 16? 2 * 24?

In short: what do you mean by "main" multipliers? What makes one pair of multipliers a better answer than another?
[ March 13, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok my gole is to build a software that
takes a numbers and splits it like this into nodes of a tree

[ March 13, 2008: Message edited by: donaldth smithts ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Does it matter whether you have


or

or

I think that probably it does not. I think that probably, all that matters is the list of prime factors at the bottom of each tree: 3 * 2 * 2 * 2. (Ignore the 1's as trivial.) But if the full tree matters... how would you know which one is correct?
[ March 13, 2008: Message edited by: Jim Yingst ]
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
each one of your three versions are correct
i need to know how to split the main input number
into one of this version
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Naive approach to get the two dividers that are the closest to each other:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by donaldth smithts:
each one of your three versions are correct
i need to know how to split the main input number
into one of this version


  • Identify your "main" input number's first (i.e., smallest) prime factor. E.g., for 48, this is 2.
  • Divide your main input number by this prime factor. Continuing with the 48 example, you have 48/2 = 24.
  • The result that you get (24) is your new "main" input number.
  • Repeat.


  • Basically, you land up factorising your number into its prime factors. For 48, you will land up with 2^4 * 3.

    - Anand


    "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
    Rory Lynch
    Ranch Hand

    Joined: Aug 03, 2007
    Posts: 95
    Rob

    Doesn't the Math.sqrt() method return a double, not an int?


    I wish that for just one time, you could stand inside my shoes.<br />You'd know what a drag it is to see you.
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    Posts: 1296
    Bonus questions:

    What is the largest prime factor of the number 600851475143 ?

    Find the sum of all the primes below two million.

    (Hint: most of the solutions to the second one that didn't involve a language that had a built in primes() method, used a Sieve of Eratosthenes.
    [ March 13, 2008: Message edited by: Garrett Rowe ]

    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    what this line does

    int lower = (int)Math.floor(sqrt);
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Have you looked at the JavaDoc for Math.floor()?
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i cant understand this definition
    "Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer."

    can you give an example?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19655
        
      18

    Originally posted by Rory Lynch:
    Rob

    Doesn't the Math.sqrt() method return a double, not an int?

    Yes it does; sqrt should be a double. I was first writing pseudo code but it was so much like actual code I changed it, but forgot that one line.

    Originally posted by donaldth smithts:
    i cant understand this definition
    "Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer."

    can you give an example?

    Basically, it rounds down to an integer number. For positive numbers, it simply cuts off all decimals. For negative numbers it cuts them off and subtracts one if the original number was not already an integer number.

    Examples:
    Math.floor(4.0) == 4.0
    Math.floor(4.2) == 4.0
    Math.floor(4.5) == 4.0
    Math.floor(4.8) == 4.0
    Math.floor(5.0) == 5.0
    Math.floor(-4.0) == -4.0
    Math.floor(-4.2) == -5.0
    Math.floor(-4.5) == -5.0
    Math.floor(-4.8) == -5.0
    Math.floor(-5.0) == -5.0
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i have writen the function that finds the small number and
    a fuction for the big number (which by multiplication give me the input number

    i know that i need to insert the small one every time on one side
    the right one into the other
    and the input one into the root.data

    i know that every tree problem is a recursive
    and it has a BNODE implemintation and a BTree implementation

    i dont know how to combine all these elements

    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19655
        
      18

    There is one improvement that immediately comes into mind:

    You've added the exact same code twice. Instead, use small inside large:

    Should you need to change the code that calculates the small number, you don't need to change it in two places.

    Keep in mind that Math.sqrt returns Double.NaN if the number is negative. Math.floor of Double.NaN returns 0, so that will result in an ArithmeticException. The same holds if the number is 0, then the root is 0 and small will be 0.
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    okay but how am i going to actually put the numbers into the tree??

    i know that i need to put the input number into the root
    (so it will be done in the BTree class)

    and the other numbers in the left and right branches( BNode class)

    and this process need to be done recursively

    till we get a prime number

    i can figure out the detailed way to implement the process
    [ March 14, 2008: Message edited by: donaldth smithts ]
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    can some one tell me what methods to build in each Class?
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i know that each small number goes to the right side
    and the big number in the left side
    an we make this proccess all over again
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19655
        
      18

    You probably want to use recursion:
    Pseudo code:
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i agree with your solution
    my problem is that i need to write every such method for BNode class
    and BTree class

    and that differs your solution
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    anyone,
    can answer me???
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Answer what, exactly?

    You've been given lot of help so far. We aren't here to do your homework for you. Rob in particular has shown you a lot of code, but he's not going to do everything for you. You need to show some effort. What have you tried so far?
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i presented my code in a previous threads

    my final question is about that:

    in order to buil a function for a binary tree i need to implement this methid
    for the BTree class and the BNode class

    and the way that i was given is only for one class

    i dont know what to do??
    as soon as i'll how to split that plan into two classes
    ill know how to change my preious code
    [ March 16, 2008: Message edited by: donaldth smithts ]
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i think that i solved it
    the problem is that i dont know how to print out a tree

    i made a single operation of putting the input number in the root
    and the rest i have done in the BNode class
    is it ok??



    [ March 16, 2008: Message edited by: donaldth smithts ]
    [ March 16, 2008: Message edited by: donaldth smithts ]
    Dan Murphy
    Ranch Hand

    Joined: Mar 29, 2005
    Posts: 126
    The Visitor pattern is often used to walk a tree. You could write a NodeVisitor to walk the tree and print out each node.


    SCJP, SCJD, SCWCD
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i have modified my code in order and put a
    System.out.println commands near the values
    and it gives me the right answers

    i need a conformations from you
    is my code ok??
    Nicholas Jordan
    Ranch Hand

    Joined: Sep 17, 2006
    Posts: 1282
    Factoring large primes, I will leave for the other posters as well as established authors on the matter. Narrowly, on is it ok?? the while (num%small!=0) does not check for runaway conditions as well as float/int conversions may not behave sensibly. For factoring primes, I would put some sanity checks around the loop and so on.

    As for it gives me the right answers, how do you know they are correct responses by the code to all conditions of use? As well, if you can formalize preconditons, you should be able to implement rudimentary test harnesses. Are you doing this to investigate prime factoring or is this an academic assignment.


    "The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i have added a line in the BNode insert method
    this is my new vertion of the insert method
    it gives me a strange resolt


    i have put a print command in the BNOde and in the BTree
    each time it inputs a number in some node

    but when i put 72 i get
    72
    8
    9
    9
    3
    3
    3
    72

    idont know why
    [ March 17, 2008: Message edited by: donaldth smithts ]
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19655
        
      18

    The 72 is the number from the first insert, with small 8 and large 9.
    The second 9 is the number from the left insert, with small 3 and large 3.
    The third 3 is the number from the left insert.
    No idea where the last 72 comes from.

    Now why you're missing some numbers because you're forgetting the insert on the right nodes.


    I've added some methods. To BTree:

    In BNode:

    Now if I print a BTree after inserting 72, this is the output:
    Looks to be just what you want, right? Well, except that prime numbers are not expanded into 1 and the prime itself



    One more remark:

    Either remove this constructor, or change it to call insert:

    Otherwise, after calling this constructor, you have the node itself but without all of its children.
    [ March 16, 2008: Message edited by: Rob Prime ]
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i think you are wrong
    i did enter the right side numbers
    i marked the line in the following code

    regarding my copy constructor
    i do need it that way because
    thats the basic function of putting a number inside a node
    whats the problem with that???

    [ March 17, 2008: Message edited by: donaldth smithts ]
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19655
        
      18

    Originally posted by donaldth smithts:
    i think you are wrong
    i did enter the right side numbers
    i marked the line in the following code

    You create the node yes, but you never call insert on that node!

    regarding my copy constructor
    i do need it that way because
    thats the basic function of putting a number inside a node
    whats the problem with that???

    Like I said, it doesn't call insert. Therefore, you store the number, but the large and small numbers are not stored in your tree.
    Also, if you call insert(int num) afterwards, it overwrites the data field so this constructor, as it is now, is completely useless. You only store the number, but as soon as you call insert all code in the constructor will be rendered useless.

    Like I said, you can fix this by just calling insert inside that constructor. That also sets the data, and makes sure your tree will be built correctly.
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i dont know how to imagine what you say
    because
    the insert method is ment for a TRee not for a node
    its a whole recursive operation for a tree
    in a constructor of the node we need only to change its data not
    to change the big number with the original number

    for me
    the constructor that i have built put a certain value inside of a node

    i cant understand how the insert method is goind to do that??
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i on each side a create a new node and then put in it
    some value
    and the values are saves in the tree

    can you show me a way thaat i could see the values of the nodes in the tree
    ??
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    can anyone help me with that???
    alex lotel
    Ranch Hand

    Joined: Feb 01, 2008
    Posts: 191
    i dont get the remark on the constructor
    ??
     
    wood burning stoves
     
    subject: how to split a number into two multipliyers