-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactor.java
More file actions
117 lines (98 loc) · 3.35 KB
/
Copy pathfactor.java
File metadata and controls
117 lines (98 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This code is my own work. I did not get any help from any online source
such as chegg.com; from a classmate, or any other person other than the instructor
or TA for this course. I understand that getting outside help from this course
other than from the instructor or TA will result in a grade of 0 in this
assignment and other disciplinary actions for academic dishonesty.
# Name : Cristian Z
# Class: CSET 1200
# Instructor: Dr. Jared Oluoch
# Programming Assignment: 9
# Problem: 1
# Date: 11/1/21
# Summary:
# Problem 1
Write a program that prompts the user to enter a positive
integer and displays all its smallest factors. (8 points).
https://youtu.be/tfrsGr7TB-g
*/
import java.util.*;
import java.util.Scanner;
public class factor{
//tree node implementation generic and simple, terms used from dictionary
static class Node
{
Node left, right;
int key;
}; //instruction video may not been detailed enough
static Node root;
//function for a new tree node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return temp;
}
//make tree of factors for the given "number"
//and store root at a reference points
//insert tears
static Node createFactorTree(Node nodeRef, int v)
{
(nodeRef) = newNode(v);
// the number is factorized
for (int i = 2 ; i < v/2 ; i++)
{
if (v % i != 0)
continue;
// factor found for left and right subtrees and return.
// go from small to high, left children will always have smaller factor
nodeRef.left = createFactorTree(((nodeRef).left), i);
nodeRef.right = createFactorTree(((nodeRef).right), v/i);
return nodeRef;
}
return nodeRef;
}
static void printOrder(Node root)
{ // Base Case
if (root == null) return;
//make a queue
Queue<Node > q = new LinkedList<>(); //learned parameter types in generic class
q.add(root);
while (q.isEmpty() == false)
{ // Print front of queue and remove from queue
Node node = q.peek();
System.out.print(node.key + " ");
q.remove();
if (node.left != null) //teach myself more and how to use object types
q.add(node.left); // simple understanding was not simple
if (node.right != null) // over complicate maybe everything
q.add(node.right);
}
}
public static void main(String[] args) {
//scanner
Scanner input = new Scanner (System.in);
System.out.println("Enter a positive whole number (Integer).");
// positive number
int number = input.nextInt();
System.out.print("Factors of " + number + " are: ");
// loop runs from 1 to whatever number thats positive
for (int i = 1; i <= number; ++i) {
// if number is divided by i
// i is the factor
if (number % i == 0) {
System.out.print(i + " ");
//return i;
}
}
//return number;
//Then the factor tree that took all week to figure out the true meaning of headaches
//ignore euclidian method for LCM
//ignore prime factoring and finding the inbetweeners
//ignore many other things that made sense but didn't seem right because of scope
root = null;
root = createFactorTree(root, number);
System.out.println("\nThe constructed factor tree");
printOrder(root);
}
}