Skip to content

Instantly share code, notes, and snippets.

@Gabryxx7
Last active June 29, 2021 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gabryxx7/12b16fa94749e775237763981dfaf3c3 to your computer and use it in GitHub Desktop.
Save Gabryxx7/12b16fa94749e775237763981dfaf3c3 to your computer and use it in GitHub Desktop.
Prime flower graph in R
library(tidyverse)
library(patchwork)
## Read first 10000 digits of prime number!
prime <- read_csv(file="http://www.naturalnumbers.org/P-100000.txt", col_names=F)
names(prime) <- c("nth_prime","prime","int") ## int = interval from previous prime number
## Function to Draw Frlower
my_flower <- function(points=5000,num_colour=9,col_option="magma",angle=pi*(3-sqrt(5)), point_size=1,...){
flower <- tibble(
n = c(1:points), ## number of points
r = sqrt(n), # In polar coordinates r is defined as sqrt(x^2 + y^2)
is_prime = n %in% prime$prime, # this might be a bit slow but we are checking whether each natural number is prime or not
colour = n%%num_colour, ## 2,3,6,12,18, seems to bring out the sprial pattern
x = r * cos(angle*n), # In polar coordinates x is calculated as r * cos(theta) where theta is the angle from the origin
y = r * sin(angle*n) # In polar coordinates y is calculated as r * sin(theta) where theta is the angle from the origin
)
prime.cnt <- flower %>%
filter(is_prime) %>%
count()
angle_deg <- if(angle==pi*(3-sqrt(5))) {
"golden angle!(137.51 degree | 2.4 radian)"
} else {
paste(round(angle*180/pi,2),"degree | ",round(angle,2),"radian")
}
## Drawing Flower (but not using Prime Number)
flower_plot <-flower %>%
filter(!is_prime) %>%
ggplot(aes(x=x, y=y, colour=colour)) +
geom_point(size=point_size) +
geom_path(size=0.01) +
scale_colour_viridis_c(end=0.8, guide="none", option=col_option) +
coord_fixed() +
theme_void(base_family="Roboto Condensed") +
labs(caption=paste(num_colour, "colours used to plot", points-prime.cnt,"dots.\nAngle Used: ", angle_deg),
subtitle="Flower Nibbled by Prime Number Bug")
## Drawing Flower (only using Prime Number)
flower_prime <-flower %>%
filter(is_prime) %>%
ggplot(aes(x=x, y=y, colour=colour)) +
geom_point(size=point_size) +
scale_colour_viridis_c(end=0.8, guide="none", option=col_option) +
coord_fixed() +
theme_void(base_family="Roboto Condensed") +
labs(caption=paste("Numbers between 1 and ",points, "have", prime.cnt," Prime Numbers\n"),
subtitle="Flower made up by Prime Numbers Only")
#You need to Print
flower_plot + flower_prime
}
my_flower(angle=0.618, points=10000, col_option = "plasma", num_colour = 10, point_size=0.5)
my_flower(angle=1.618, points=10000, col_option = "plasma", num_colour = 10, point_size=0.5)
my_flower(angle=0.3, points=10000, col_option = "plasma", num_colour = 10, point_size=0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment