Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibly add a primes key to the new integer keyboard? #7565

Closed
rdstern opened this issue Jun 17, 2022 · 14 comments · Fixed by #7638
Closed

Possibly add a primes key to the new integer keyboard? #7565

rdstern opened this issue Jun 17, 2022 · 14 comments · Fixed by #7638
Assignees
Projects
Milestone

Comments

@rdstern
Copy link
Collaborator

rdstern commented Jun 17, 2022

This is only (at this stage) if it is really easy (less than 45 minutes?) for @lilyclements to code! Then @anastasia-mbithe or @Wycklife could implement.
It changes the current prime key to is_prime and adds a vectorised key called prime to the keyboard.

a) We already have a next prime key, so (I presume) this could be done via purrr so the command prime(from = n) gives a set of prime numbers of length equal to the data frame. Perhaps it needs a second argument such as values = m, where the default is the length of the data frame? @lilyclements is this a really simple bit of code that could become the prime key?

The website on the first 50 million primes says "Usually it is faster to run a program on your own computer than to download them, but by popular demand, here they are!"

So that's one reason why this function would be useful. It may also show how purrr avoids "old fashioned" loops?

This site also gives various functions on primes. I don't know if there is any help here?

@rdstern rdstern added this to the 0.7.x milestone Jun 17, 2022
@anastasia-mbithe anastasia-mbithe added this to To do in Developers via automation Jun 22, 2022
@lilyclements
Copy link
Contributor

@rdstern I've had a look at this, mainly looking at stackoverflow.

So do we want two functions? One for prime, and one for is_prime, where prime gives a vector containing prime numbers, and is_prime checks if the current variable is a prime number.

Had a look on stack overflow, and to find is_prime, I think the following is a good solution:

is_prime <- function(x){
  div <- 2:ceiling(sqrt(x))
  !any(x %% div == 0)
}

The prime options found generally give a to parameter. Having this m option of to give the next m primes is a little tricky so the code uses the nextprime function. I give two options: give a from and to, or give a from and n for the number of primes you want after a given value.

list_prime <- function(from, to, n){
  is_prime <- Vectorize(is_prime)
  if (missing(n)){
    num <- from:to
    return(num[is_prime(num)])
  } else if (missing(to)){
    np <- NULL
    for (i in 1:n){
      np[i] <- as.integer(gmp::nextprime(from))
      from <- np[i]
    }
    return(np)
  }
}

list_prime(from = 1, to = 100)
list_prime(from = 8, n = 10)

@rdstern
Copy link
Collaborator Author

rdstern commented Jul 4, 2022

@lilyclements many thanks. If we include the function, then I assume we could have a primes key on our integer keyboard. @anastasia-mbithe can you include this?

@rdstern rdstern changed the title Possibly add a prime key to the new integer keyboard Possibly add a primes key to the new integer keyboard? Jul 4, 2022
@anastasia-mbithe
Copy link
Contributor

@rdstern and @lilyclements, This is great! However, my biggest worry is how we will incorporate this into the calculator. If you look at the calculator code it's implemented differently from the other dialogs, and it's effective with **single functions. (When I say single functions I mean functions like Factorize(), nextPrime(), isprime() etc)
That's why last time @Wycklife and I had a hard time with Factorize() until we landed on the DescTools package. In the meantime, I'll look for a way around it. @rdstern, can this fit in any other dialog apart from the calculator?

@rdstern
Copy link
Collaborator Author

rdstern commented Jul 5, 2022

@anastasia-mbithe that's an interesting idea. I was hoping that if @lilyclements writes this as an R-Instat function, then the calculator could simply call it? Or might there even be a (for now) trivial package that we then load in the same way as other packages. We will soon be having our own packages, so might this be a simple start?
@anastasia-mbithe did you also see my other suggestions yesterday for being able to produce binary, octal and hexadecimal numbers - perhaps even as more keys there. How did you like those additions to the keyboard, as another interesting topic for the Maths camps and clubs? (I found that because the 1000 primes in the Lists in File > New Data Frame gives a list in binary.) Then I was quite excited to re-discover the as.hexmode and as.octmode features in base r!

When discussing with @Wycklife there is another (small?) improvement in the calculator that would be great to add.
We now have the down-arrow pull-down option on keys. Could this be added to the Integer keyboard first - then to others. This is instead of needing 2 R-help buttons for the integer keyboard (and also for others when there are 2 and some when there should be. It could also be clearer, i.e. the main key can be called R-Help and links to the main package. In the pull-down the label could just be the successive names of the package, e.g. desctools.

Finally, while improving the calculator, could the default be to save the variable, i.e. the default is with the saving checkbox ticked.

You may want to write out some of these suggestions as separate issues, so they can be spelled out in more detail?

@lilyclements
Copy link
Contributor

@rdstern @anastasia-mbithe once the new lists/data frame PR is merged (hopefully soon as it’s ready for review) I can add these new prime functions to the stand-alone-functions.R file. Then we can make the changes to the calculator with the primes.

@rdstern ill look into the binary, octal, etc functions and comment on the issue related to them about it

@anastasia-mbithe
Copy link
Contributor

@anastasia-mbithe that's an interesting idea. I was hoping that if @lilyclements writes this as an R-Instat function, then the calculator could simply call it? Or might there even be a (for now) trivial package that we then load in the same way as other packages. We will soon be having our own packages, so might this be a simple start? @anastasia-mbithe did you also see my other suggestions yesterday for being able to produce binary, octal and hexadecimal numbers - perhaps even as more keys there. How did you like those additions to the keyboard, as another interesting topic for the Maths camps and clubs? (I found that because the 1000 primes in the Lists in File > New Data Frame gives a list in binary.) Then I was quite excited to re-discover the as.hexmode and as.octmode features in base r!

When discussing with @Wycklife there is another (small?) improvement in the calculator that would be great to add. We now have the down-arrow pull-down option on keys. Could this be added to the Integer keyboard first - then to others. This is instead of needing 2 R-help buttons for the integer keyboard (and also for others when there are 2 and some when there should be. It could also be clearer, i.e. the main key can be called R-Help and links to the main package. In the pull-down the label could just be the successive names of the package, e.g. desctools.

Finally, while improving the calculator, could the default be to save the variable, i.e. the default is with the saving checkbox ticked.

You may want to write out some of these suggestions as separate issues, so they can be spelled out in more detail?

@rdstern, yeah I saw the comment and I have been testing them and I also think they are interesting to add to the integer keyboard. I can get started on adding them before @lilyclements and I figure out how to add the Primes function to the calculator.

@N-thony
Copy link
Collaborator

N-thony commented Jul 11, 2022

@lilyclements @anastasia-mbithe any progress on this?

@anastasia-mbithe
Copy link
Contributor

@rdstern and @lilyclements, there is this package called primes I just came across, I think it will help us fix this issue.
There are different functions that include;
generate_n_primes(n,min,max)
k_tuple(min, max, tuple)
nth_prime(x)

What do you think about it and which functions can we consider?

@rdstern
Copy link
Collaborator Author

rdstern commented Jul 18, 2022

Good find @anastasia-mbithe it looks great. I'm delighted as there's lots you can add from here. It will be good to add the package first, so this improvement gets into the July release. Can you start with that.
Then perhaps suggest a design for the expanded keyboard that uses as many of the functions as seems convenient.

Note the important generate_n_primes can include (for us) the n, because that's the length of the data frame. I really like the nth_prime and the prime_count, because they (I think) can have vectors as input. We will have to be imaginative on what we call the keys, so it isn't too long = and also how we use some of the functions, that may (or may not) allow columns as input. That's one reason for starting by just adding the package into the new version - then it is easier to play. Though good to add it to your version now to try it out.

@anastasia-mbithe
Copy link
Contributor

Thanks for the feedback.
Just a small correction, there is generate_n_primes(n), and generate_primes(min, max).
Yes, I agree we can start with generate_n_primes().

@lilyclements
Copy link
Contributor

@anastasia-mbithe nice find! Looks like a great package :)

@anastasia-mbithe
Copy link
Contributor

@rdstern, Unfortunately generate_n_primes() does not accept vectors. is it okay to have nth_prime() for the start?

@lilyclements
Copy link
Contributor

@anastasia-mbithe this is interesting.

@anastasia-mbithe @rdstern have we considered having the calculator to have the ability to do things that don't necessarily require the data frame for (and, say, disabiling the data frame selector when those buttons are chosen?). Perhaps this defeats the point but I am intrigued why we wouldn't.
Could generate_n_primes be something that is more appropriate in the "generate sequences" dialog?

@rdstern
Copy link
Collaborator Author

rdstern commented Jul 19, 2022

@anastasia-mbithe I am still happy with the generate_n_primes in the calculator, where it would give the command just like that including the n parameter, either as a number of sa nrows(dataframe).
Intriguing to also add a sequences button (as @lilyclements suggests) into the regular sequences dialogue and also include it - and other sequences - there too. I like that idea!!!

Developers automation moved this from In progress to Done Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging a pull request may close this issue.

4 participants