Little useless-useful R functions – Countdown number puzzle

The famous countdown game loved among mathematicians and people adoring numbers and why not find a way to check for solutions.

So the game is (was) known as a TV show where then host would give a random 3-digit number and the contestants would draw 6 random numbers from stack of numbers. Given the time limit, the winner was the one who would create a formula matching the result or being closest.

Many ways, tips, tricks and optimisations were already considered, maybe the most famous was the Reverse Polish notation where operators follow their operands and is a great fit for the game.

With useless functionality, I have decided to use permuteGeneral function from RcppAlgos or same functionality could be achieved with combn function.


countDown_puzzle <- function(six, res_num) {
    oper <- c("+","-","/","*")
    res <- 0
    d2 <- permuteGeneral(six)
    for (i in 1:nrow(d2)){
      for (o in 1:1000){
        r <- paste0(as.integer(d2[i,1]),' ',as.character(sample(oper,1)),' (',as.integer(d2[i,2]),' ',as.character(sample(oper,1)),' ((', as.integer(d2[i,3]),' ',as.character(sample(oper,1)),as.integer(d2[i,4]),') ',as.character(sample(oper,1)), 
as.integer(d2[i,5]),')',as.character(sample(oper,1)),as.integer(d2[i,6]), ') ', sep ="")
        res <- eval(parse(text=r))
        if(res == res_num){
          print(paste0("Solution: ", r, ' with result of: ', res, ' for given vector: ', paste(six, collapse = " "), sep=""));       

And simply run the function:

countDown_puzzle(c(100,9,4,1,7,25), 463)

If you want to generate also the numbers and a random solution:

number_pool <- c(1:11, 25, 50, 75, 100, 200)
six <- sample(number_pool, 6, replace=FALSE)
res_num <- sample(100:999,1)

# run function
countDown_puzzle(six, res_num)

There are many immediate optimisations to this game, such as:
– there is no need to multiply or divide by 1
– intermediate solutions become non-integer
– intermediate solution become negative number
– redundant calculations as: x+y = x, respectively for subtractions.
– and many more.

Non of there were taken into consideration, but it could be a great start for optimisation.

As always, code is available at Github.

Happy R-coding!

Tagged with: , , , , , , ,
Posted in Uncategorized, Useless R functions
2 comments on “Little useless-useful R functions – Countdown number puzzle
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Liked by 1 person

  2. […] Tomaz Kastrun has a fun puzzle for us: […]

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Follow TomazTsql on
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 (


Top SQL Server Bloggers 2018

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


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


Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

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


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


Bringing meaning to data & insights through experiences users love


R news and tutorials contributed by hundreds of R bloggers

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 Server Insane Asylum (A Blog by Pat Wright)

Information about 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.

%d bloggers like this: