r/dailyprogrammer • u/Elite6809 1 1 • Jun 14 '14
[6/14/2014] Challenge #166b [Intermediate] Prime Factor Trees
(Intermediate): Prime Factor Trees
Every number can be represented as the product of its prime factors. These are all of the prime numbers which the number is divisible by - if a number has no prime factors except itself, then it is prime (because it cannot be divided by any other number.) Finding the prime factor representation of a number comes in handy in quite a few ways - one of which is being able to easily find the Greatest Common Divisor.
One of the first techniques schoolchildren learn to find a number's prime factors is a technique known as factor trees. To create a factor tree, write down the number you are factoring first.
60
Then, find a number that divides this cleanly, and find the answer - 60 can be divided by 4 to get 15, for example. Once we've done that, write those two numbers under 60 on 'branches', like so:
60
|
4--+--15
Then, do the same for each of those numbers, too:
60
|
4--+--15
|
2-+-2
And finally:
60
|
4--+--15
| |
2-+-2 3-+-5
Once a prime number (such as the bottom row) is created, you can't factor any further, so you stop.
Your challenge is, given a number, generate its factor tree.
Formal Inputs and Outputs
Input Description
You will be given a number N which you are to generate a factor tree for.
Output Description
Print the factor tree in a similar format to the ones above.
Challenge
Challenge Input
1767150
Sample Challenge Output
There are a lot of different ways to display a factor tree for some numbers. Here are some examples.
1767150
|
1309-----+-----1350
| |
77-+--17 45---+---30
| | |
7+-11 9-+--5 6-+--5
| |
3+-3 2+-3
1767150
|
1350----+-----1309
| |
45---+---30 77-+--17
| | |
5-+-9 6-+--5 7+-11
| |
3+-3 2+-3
Notes
If you're having trouble with the tree printing logic, that's fine - you can skip that if you want. Print it a different way that's easier to format.
5
u/Elite6809 1 1 Jun 14 '14 edited Jun 15 '14
Ruby again. I decided to document it so you can use my tree printing logic if you so wish. It generates ugly trees when one side of the branch is much wider than the other, however.
3
u/Godspiral 3 3 Jun 14 '14
The tree thing looks like its own challenge, but J has its own boxed display of trees:
or
reddit ": ,. L:1 ,. fac 1767150
version with "likely" decreasing factor order
fac =: 3 : 'q =. q: y if. 1<#q do. (y ; [: fac "0 each y (] , %) ([: */ ] }.~ <.@:-:@:#)) q else. y end.'
reddit ": ,. L:1 ,. fac 1767150
3
u/rcxdude Jun 15 '14
Rust (rustc 0.11.0-pre (e55f64f 2014-06-09 01:11:58 -0700))
example output:
2
u/[deleted] Jun 14 '14 edited Jan 02 '16
2
u/PrintfReddit Jun 15 '14
Short and sweet PHP (and my first submission to this subreddit!)
1
u/Frichjaskla Jun 14 '14
I was lazy and chose to output to dot rather than make an ascii tree
1
u/ehcubed Jun 14 '14
Python 3.3. For printing the tree, I decided to print it sideways using a reversed inorder traversal. Factors are chosen at random.
Code:
Sample Output (for N = 1767150):
1
u/Frigguggi 0 1 Jun 15 '14
Java. No tree yet, but it prints the prime factors. Hopefully I will update this with more impressive output:
Output:
1767150
17 * 7 * 11 * 3 * 2 * 5 * 5 * 3 * 3
1
u/poeir Jun 15 '14
Python implementation. I'm kind of phoning this one in, I'm afraid. I did learn about ete2, which gives nice tree formatting, though it's horizontal instead of vertical. And it could sorely use some optimization.
1
u/IceDane 0 0 Jun 15 '14
I meant to draw a graph using graphviz or some such, but the libraries I found didn't make it too easy. I then stumbled upon a link which showed me pretty smart(and obvious in retrospect) way of visualizing it.
Here's the output. All the leaf nodes are the primes.
1
u/Reverse_Skydiver 1 0 Jun 16 '14
Late as usual. Made my own tree structure and made it print in a "nice" way. Done in Java.
Sample input: 138567
Sample output: http://i.imgur.com/w7t2ymA.jpg
1
u/toodim Jun 20 '14
Python 3. Didn't print a full tree, just prints the final prime factorization.
1
u/TheSuperWig Jun 20 '14 edited Jun 21 '14
C++ currently without a fancy tree output.
Input: 1767150
Output: 1767150 = 2*3*3*3*5*5*7*11*17
EDIT: Version using the output formatting stolen from /u/IceDane:
1
u/dohaqatar7 1 1 Jun 23 '14
I'm more than a little late on this challenge, so I'm not expecting anyone to read this, but I could really use some legitimate revue on my code. Haskell is fairly new to me, so any help would be greatly apreciated!
Haskell Solution
Sample Output
1
u/CLope0204 Jul 09 '14
C# Version using recursivity
Primamid tree: