Created
August 7, 2011 08:25
-
-
Save ajdamico/1130192 to your computer and use it in GitHub Desktop.
calculate the percent of fractions that are repeating vs. non-repeating with R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# calculate the percent of fractions that are repeating vs. non-repeating | |
# install stringr if you don't already have it | |
#install.packages("stringr") | |
library( stringr ) | |
options( digits=22 ) | |
# this program looks at.. | |
# 1/1, 1/2, 1/3, 1/4.. up to 1/x | |
# then goes to | |
# 2/1, 2/2, 2/3, 2/4.. up to 2/x | |
# and continues increasing the numerator until | |
# x/1, x/2, x/3, x/4.. up to x/x | |
# ..and for each of these fractions, determines whether or not it's repeating | |
# but! any of the above fractions where the numerator is >= the denominator don't count! | |
percent_repeating_fractions <- function( x ) { | |
# create an x by x matrix full of zeroes to store the results | |
y <- matrix( 0 , x , x ) | |
# loop through all natural number numerators & denominators from 1 to x | |
for ( numerator in 1:x ){ | |
for ( denominator in 1:x ){ | |
# here's the current fraction | |
current_fraction <- numerator / denominator | |
# throw out matrix cells where the current_fraction is >= 1 | |
if ( current_fraction >= 1 ) { | |
y[ numerator , denominator ] <- NA | |
} else { | |
# according to http://en.wikipedia.org/wiki/Repeating_decimal | |
# a fraction is non-repeating if its denominator is a factor of either two or five | |
if ( | |
# use the modulus operator to test for perfect division | |
((denominator %% 2) == 0) | | |
((denominator %% 5) == 0) | |
) { | |
# mark non-repeating cells with a one | |
y[ numerator , denominator ] <- 1 | |
} | |
} | |
} | |
} | |
# calculate the number of ones in the final matrix | |
ones <- sum( y , na.rm = T ) | |
# calculate the total number of cells that weren't thrown out | |
valid <- sum( !is.na(y) ) | |
# return the percent of non-repeating fractions out of all fractions tested | |
(ones / valid) | |
} | |
# run this function for different sized x by x matricies up to z | |
# choose how high up you want to go (anything above 1000 becomes processor-intensive) | |
z <- 500 | |
for ( i in 2:z ){ | |
# calculate the percent repeating fractions | |
result <- percent_repeating_fractions( i ) | |
# make the result look nice | |
formatted_result <- result * 100 | |
print( | |
paste( | |
"the" , | |
str_pad( i , width = nchar( z ) ) , | |
"by" , | |
str_pad( i , width = nchar( z ) ) , | |
"matrix is" , | |
str_pad( formatted_result , width = 16 , side = "right" ) , | |
"percent non-repeating fractions" | |
) | |
) | |
} | |
# the output makes sense, since out of the denominators ending in 0 through 9, | |
# denominators 0, 2, 4, 5, 6, and 8 (or 6 out of 10) will be non-repeating | |
# in conclusion, | |
# one could argue that about sixty percent of fractions are non-repeating! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment