# how to split a number into two multipliyers

Sheriff

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*

Sheriff

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 ]

"I'm not back." - Bill Harding, *Twister*

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

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

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

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

Sheriff

"I'm not back." - Bill Harding, *Twister*

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

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

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

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

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 ]

Pseudo code:

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

Sheriff

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?

"I'm not back." - Bill Harding, *Twister*

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 ]

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 ]

SCJP, SCJD, SCWCD

**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."

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 ]

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

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 ]

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

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??