4 kyu
Permutational Primes
278 of 502raulbc777
Loading description...
Algorithms
Sorting
Permutations
Mathematics
Memoization
Data Structures
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
The Ruby test suite includes three additional tests that are not present in the other language versions:
However, these tests are incorrect. For instance,
1979
has four prime permutations:[1997, 7919, 9719, 9791]
.No. See this suggestion for a clearer explanation.
You should generate all primes <=
n_max
and group these & only these primes that are similar by their permutations.Given your case, we do not include
7919, 9719, 9791
here because they are greater than2000
. So1979
only has one prime permutation equal to and below 2000:[1997]
.Ruby is not the odd-one-out here, these values are also tested in the other languages in the submission test suite & have the same answers. Ruby just test these values earlier for a lesser surprise.
Haskell translation
Approved in Haskell!
Very good Kata! Thanks to author!!!
Great kata of the series to practice with, quite challenging indeed.
cool kata!
Nice kata Raul as always!
Just a small suggestion - to illustrate the part about all the permutations of each prime needing to be less or equal than
n_max
not just the "initial" prime:Currently all examples in description involve
n_max = 1000
, therefore all the 1/2/3 digit numbers obtained by permutation will always be < 4 digits < 1000, so the above requirement is not illustrated in such cases.If you gave a single prime example with, e.g.,
n_max = 6000
you could illustrate the expected behaviour - something like:"For
n_max = 6000, k_perms = 3
the primep = 2851
is not a valid candidate because although it is itself<= 6000
, and does havek = 3
other permutations that are prime:5281, 5821, 8521
, not all of these 3 permutations are <= 6000."thanks so much for this, its really easy to miss and i never would have noticed it if i hadnt read your comment
although this comment helped me, it still does no fully describe the actual criterion, which is really hard to deduce from the description: if a prime permutation is above
n_max
, it does not count towardsk_perms
and the initial number is still a valid candidate. for example forn_max = 2000, k = 1
,1979
is a valid solution because it has a single prime permutation<= 2000
, that is1997
. This is despite the fact that there are other prime permutations of1979
that are above2000
:7919, 9719, 9791
The prime below 1000 with three prime permutations should include 136.
Do I say it right? I'm so confused.
136 itself isn't prime though. You're looking for prime numbers with exactly 3 prime permutations (not including itself).
If the permutation of a number is above the n_max, we don't count it, right?
Yes, the description is explicit about that.
I preferred to make sure I understood everything, thank you
also the kata that got me to 10k honor ;)
I tried to approve it. I didn't see at first glance that you did it with the new test frame. Sorry for that.
python fork with new test framework
Already approved.
Julia translation
approved+1
Cheers!
This comment has been hidden.
779 is not prime. 19*41 = 779
C# Translation
Done +1
Go translation
Approved +1
D translation
Approved +1
Rust translation
Approved + 1
Ruby 3.0 should be enabled
Enabled in this fork
The limit is actually inclusive, and there should be fixed tests to explicitly verify the correct behavior, e.g.
permutational_primes(17209, 4) == [50, 1013, 13597]
.Please specify language(s). JS seems unaffected, if I'm reading correctly, and testing seems to confirm that.
Can confirm this issue in Rust. I was interpreting "below" as "strictly less than" and had a hard time figuring out why my solution was failing the tests. Then I noticed that the case it failed on had the upper limit as a permutation of one of the primes and that the test was counting that:
It sounds like it's the Rust test implementation that's wrong, not the Kata description, based on the fact that the JS version doesn't have this issue. So either the Rust test should be fixed...or maybe the Kata description should just be changed to call out the different expectation for Rust solutions.
Ruby had this issue, fixed in this fork
The kata asks to return the number of, smallest, and largest (permutational) prime. As written, I would give different answers to the ones desired.
To illustrate the issue, consider the example n = 1000, k = 3:
149 ==> 419 ==> 491 ==> 941 179 ==> 197 ==> 719 ==> 971 379 ==> 397 ==> 739 ==> 937
for which we are asked to find [3, 149, 379]. However, looking at the above 12 prime numbers that are all below 1000, clearly 971 is the largest, and without further specification, I would say the answer is [3, 149, 971], not [3,149,379].
One way to "fix this up" is to say that given a set of primes related by digit permutation, in this kata "the" permutational prime is considered to be the smallest prime in this set. I do not think this is great terminology however, as all primes in a set of permutations are "permutational". In my opinion it would be better to either rewrite the tests to reflect the answer to the question as currently stated (though slightly easier to answer in my current approach at least), or to just specify that we want to find the smallest and biggest of the smallest prime in each set of primes related by permutations.
That line there explains why you should return 379 and not 971.
You're completely right, thanks! I just noticed that, even though before I triple checked the definitions at the start, and the task description. I think it would still be good not to write in the task that one should find "the largest of such primes", as for instance all 12 in the example are permutational primes.
This still links to the original Kata which looks to be unpublished? Which I didn't even notice and solved it anyway and only discovered that when I noticed I could not submit it.
Description edited thanks.
This comment has been hidden.
JavaScript Translation submitted.
Ruby translation submitted, seems to be working.
ok , let's see
Approved!
Has occurred to me that this kata might not be showing up on people's radar much because of its name... it ends in PrimesII. That means it won't show up in searches for Primes katas.. or even in "similar kata" except for permutation kata. You might want to rename it Permutational Primes II (if that's possible?) :)
Kata title updated.
I also updated the ruby version, as it was missing the checker function...
Oh, and you don't need to write a prime checker in ruby, just use this:
This comment has been hidden.
This comment has been hidden.
Ah. Thanks very much for the response. Yeah pretty bad oversight lol.
As for the .prime? vs. mine, I don't disagree, but I seem to remember that's what the benchmarking was showing (on codewars, not locally). I could be wrong, but codewars does seem to occasionally exhibit some sort of odd behavior; although given how small the primes here are it might just be more a function of requiring 'prime' adding some overhead rather than anything else.
Is it intentional for reference solution in ruby to be amongst the slowest, to add some performance requirements, or is updating to a much faster code like ex. an option?
I'm probably misunderstanding the question here, but can someone explain this?
In the test for
find_prime_kPerm(2000,1)
, I get[45, 13, 1999] should equal [64, 13, 1979]
. The Kata expects the max prime to be1979
. But as far as I can tell,1979
has4
prime permutations as follows:1979 -> 9791, 7919, 1997, 9719
. Therefore, I cannot understand how the the Kata expects the max prime to be1979
in this case.Never mind. Solved it. All permutations must be <= the limit given.
This comment has been hidden.
I don't think the kata description is clear on this point. I suggest an example added in the description and to the sample test cases.
There are no mentions how
0
s and duplicates are handled.The kata only accept a permutational prime if it has the same amount of digits as the number you're considering. There are ambiguities to this, e.g
107
only counts as having 1 permutation (701
) while17
and71
are not considering to be related.(Then of course, you have numbers like
5881
.)Description updated.
You should encapsulate your global variables inside a closure so it doesn't clash with user global variables (especially since you're using a function named
primes
which is highly likely to cause a collision).Fixed.
approved
Thanks!!
.
This comment has been hidden.
Suggestions received. It will be fixed in four hours.
Looks like more than 400 hours has been passed, but this hasn't been fixed yet ;-)
Description updated.