Little useless-useful R functions – Finding substrings in number Pi

They say that number Pi holds all infinite possibilities, all the words, all the combinations. Well, easier said to be done. So let’s put this to a test.

This is my translational table.


We will do this in the following steps (for the word “eggs”):

  1. Encode the word EGGS to numbers. E = 5, G = 7, G = 7 and S = 19. Together concatenated we get the string of 57719.
  2. We store a veeery long string of PI number.
  3. Start looking in PI number for substring of “57719”.

Well, let’s create a function to do just that


library(Rmpfr)

encode_a1z26 <- function(s) {
  s_clean <- gsub("[^A-Za-z]", "", toupper(s))
  if (nchar(s_clean) == 0) stop("No letters found in input.")
  letters_vec <- strsplit(s_clean, "", fixed = TRUE)[[1]]
  nums <- match(letters_vec, LETTERS)          
  paste0(nums, collapse = "")
}


pi_fraction_digits <- function(n_digits) {
  # bits of precison ~ n_digits * log2(10)
  precBits <- ceiling(n_digits * log2(10)) + 32L
  pi_mpfr <- Const("pi", precBits)                        
  s <- formatMpfr(pi_mpfr, digits = n_digits + 2L, scientific = FALSE, base = 10L)
  s <- gsub("\\.", "", s, fixed = FALSE)
  frac <- substr(s, 2L, n_digits + 1L)
  if (nchar(frac) < n_digits) {
    stop("To low precision; did not get requested number of digits")
  }
  frac
}

find_in_pi <- function(pattern, n_digits) {
  if (!grepl("^[0-9]+$", pattern)) stop("Pattern must be digits!")
  t0 <- proc.time()[["elapsed"]]
  frac <- pi_fraction_digits(n_digits)
  loc <- regexpr(pattern, frac, fixed = TRUE)
  elapsed <- proc.time()[["elapsed"]] - t0
  
  #getting the positions
  if (loc[1] != -1) {
    start_pos <- as.integer(loc[1])                
    end_pos   <- start_pos + nchar(pattern) - 1L
    list(found = TRUE,
         start = start_pos,
         end   = end_pos,
         digits_scanned = n_digits,
         seconds = elapsed)
  } else {
    list(found = FALSE,
         start = NA_integer_,
         end   = NA_integer_,
         digits_scanned = n_digits,
         seconds = elapsed)
  }
}

find_phrase_in_pi <- function(phrase, n_digits) {
  pat <- encode_a1z26(phrase)
  res <- find_in_pi(pat, n_digits)
  res$pattern <- pat
  res$phrase  <- phrase
  res
}

and finally, to run the function(s):

## Run functions
word <- "eggs"
encoded_word <- encode_a1z26(word)
cat("Encoded ",word," ->", encoded_word, "\n")  
result_today <- find_in_pi(encoded_word, 1e5)
print(result_today)

And see that the word “EGGS” as the number 57719, appears on Pi Number on 6026th till 6030th position. Pretty useless 🙂

As always, the complete code is available on GitHub in  Useless_R_function repository. The first version is here (filename: Find_substring_in_Pi.r).

Check the repository for future updates!

Stay healthy and happy R-coding!

Tagged with: , , ,
Posted in R, Uncategorized, Useless R functions

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started