From 0e2dbc51e84d8961c6d0d27a0f1d384d59191b7a Mon Sep 17 00:00:00 2001 From: James Schloss Date: Fri, 29 Jun 2018 12:34:49 +0900 Subject: [PATCH 1/3] simplifying julia code --- chapters/monte_carlo/code/julia/monte_carlo.jl | 17 ++++++++++++----- chapters/monte_carlo/monte_carlo.md | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/chapters/monte_carlo/code/julia/monte_carlo.jl b/chapters/monte_carlo/code/julia/monte_carlo.jl index 818d60efb..efdb9083d 100644 --- a/chapters/monte_carlo/code/julia/monte_carlo.jl +++ b/chapters/monte_carlo/code/julia/monte_carlo.jl @@ -1,5 +1,8 @@ # function to determine whether an x, y point is in the unit circle -function in_circle(x_pos::Float64, y_pos::Float64, radius::Float64) +function in_circle(x_pos::Float64, y_pos::Float64) + + # Setting radius to 1 for unit circle + radius = 1 if (x_pos^2 + y_pos^2 < radius^2) return true else @@ -8,20 +11,24 @@ function in_circle(x_pos::Float64, y_pos::Float64, radius::Float64) end # function to integrate a unit circle to find pi via monte_carlo -function monte_carlo(n::Int64, radius::Float64) +function monte_carlo(n::Int64) pi_count = 0 for i = 1:n point_x = rand() point_y = rand() - if (in_circle(point_x, point_y, radius)) + if (in_circle(point_x, point_y)) pi_count += 1 end end - pi_estimate = 4*pi_count/(n*radius^2) + # This is using a quarter of the unit sphere in a 1x1 box. + # The formula is pi = (box_length^2 / radius^2) * (pi_count / n), but we + # are only using the upper quadrant and the unit circle, so we can use + # 4*pi_count/n instead + pi_estimate = 4*pi_count/n println("Percent error is: ", signif(100*(pi - pi_estimate)/pi, 3), " %") end -monte_carlo(10000000, 0.5) +monte_carlo(10000000) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 8b283a3bd..3933dfe15 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -27,7 +27,7 @@ $$ This means, $$ -\text{Area}_{\text{circle}} = \text{Area}_{\text{square}}\times\text{Ratio} = 4r^2 \times \text{ratio} +\text{Area}_{\text{circle}} = \text{Area}_{\text{square}}\times\text{Ratio} = 4r^2 \times \text{Ratio} $$ So, if we can find the $$\text{Ratio}$$ and we know $$r$$, we should be able to easily find the $$\text{Area}_{\text{circle}}$$. From 285ebec11bd429ca43902b8a4716eacac3427759 Mon Sep 17 00:00:00 2001 From: leios Date: Sat, 30 Jun 2018 06:17:08 +0900 Subject: [PATCH 2/3] Revert "simplifying julia code" This reverts commit 0e2dbc51e84d8961c6d0d27a0f1d384d59191b7a. --- chapters/monte_carlo/code/julia/monte_carlo.jl | 17 +++++------------ chapters/monte_carlo/monte_carlo.md | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/chapters/monte_carlo/code/julia/monte_carlo.jl b/chapters/monte_carlo/code/julia/monte_carlo.jl index efdb9083d..818d60efb 100644 --- a/chapters/monte_carlo/code/julia/monte_carlo.jl +++ b/chapters/monte_carlo/code/julia/monte_carlo.jl @@ -1,8 +1,5 @@ # function to determine whether an x, y point is in the unit circle -function in_circle(x_pos::Float64, y_pos::Float64) - - # Setting radius to 1 for unit circle - radius = 1 +function in_circle(x_pos::Float64, y_pos::Float64, radius::Float64) if (x_pos^2 + y_pos^2 < radius^2) return true else @@ -11,24 +8,20 @@ function in_circle(x_pos::Float64, y_pos::Float64) end # function to integrate a unit circle to find pi via monte_carlo -function monte_carlo(n::Int64) +function monte_carlo(n::Int64, radius::Float64) pi_count = 0 for i = 1:n point_x = rand() point_y = rand() - if (in_circle(point_x, point_y)) + if (in_circle(point_x, point_y, radius)) pi_count += 1 end end - # This is using a quarter of the unit sphere in a 1x1 box. - # The formula is pi = (box_length^2 / radius^2) * (pi_count / n), but we - # are only using the upper quadrant and the unit circle, so we can use - # 4*pi_count/n instead - pi_estimate = 4*pi_count/n + pi_estimate = 4*pi_count/(n*radius^2) println("Percent error is: ", signif(100*(pi - pi_estimate)/pi, 3), " %") end -monte_carlo(10000000) +monte_carlo(10000000, 0.5) diff --git a/chapters/monte_carlo/monte_carlo.md b/chapters/monte_carlo/monte_carlo.md index 6ee8aab6f..bbd33671f 100644 --- a/chapters/monte_carlo/monte_carlo.md +++ b/chapters/monte_carlo/monte_carlo.md @@ -27,7 +27,7 @@ $$ This means, $$ -\text{Area}_{\text{circle}} = \text{Area}_{\text{square}}\times\text{Ratio} = 4r^2 \times \text{Ratio} +\text{Area}_{\text{circle}} = \text{Area}_{\text{square}}\times\text{Ratio} = 4r^2 \times \text{ratio} $$ So, if we can find the $$\text{Ratio}$$ and we know $$r$$, we should be able to easily find the $$\text{Area}_{\text{circle}}$$. From a82cb1811daf82b3a783ae5a020620d951a92bb2 Mon Sep 17 00:00:00 2001 From: leios Date: Sat, 30 Jun 2018 06:20:32 +0900 Subject: [PATCH 3/3] modifying Julia DFS inorder function to allow for different tree configurations. --- chapters/tree_traversal/code/julia/Tree.jl | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/chapters/tree_traversal/code/julia/Tree.jl b/chapters/tree_traversal/code/julia/Tree.jl index 92528bbc0..79f5b4cd6 100644 --- a/chapters/tree_traversal/code/julia/Tree.jl +++ b/chapters/tree_traversal/code/julia/Tree.jl @@ -25,20 +25,20 @@ function DFS_recursive_postorder(n::Node) println(n.ID) end -# This assumes only 2 children +# This assumes only 2 children, but accounts for other possibilities function DFS_recursive_inorder_btree(n::Node) - if (length(n.children) > 2) - println("Not a binary tree!") - exit(1) - end - - if (length(n.children) > 0) - DFS_recursive_inorder_btree(n.children[0]) + if (length(n.children) == 2) + DFS_recursive_inorder_btree(n.children[1]) println(n.ID) + DFS_recursive_inorder_btree(n.children[2]) + elseif (length(n.children) == 1) DFS_recursive_inorder_btree(n.children[1]) - else println(n.ID) + elseif (length(n.children) == 0) + println(n.ID) + else + println("Not a binary tree!") end end @@ -96,6 +96,11 @@ function main() println("Using queue-based BFS:") BFS_queue(root); + + println("Creating binary tree to test in-order traversal.") + root_binary = create_tree(3,2) + println("Using In-order DFS:") + DFS_recursive_inorder_btree(root_binary) end main()