7 kyu
Find the divisors!
39,994 of 89,419kwy95
Loading description...
Mathematics
Algorithms
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.
This comment has been hidden.
Not a kata issue!
Your code assumes all odd integers are primes, which is wrong
Your code returns
[]
for2
which is a prime itsdelf[Haskell fork]
The random tests were pretty weak (only testing numbers up to 100), and have been rewritten.
Have thoughts...
In Codewars, you can usually see the inputs by printing them out.
How can I see which input causes my solution to fail?
Solving a Kata
You can think of Hajime as something along the lines of "Let's start the exercise!"
I don't know math incredibly well, but I feel like this was really poorly worded. Wasn't sure what it wanted me to do for a while.
Lua translation!
Approved
There is a problem with one assertion in the Elixir version. The exercise requires to return a sorted list, but it fails when it gets [1, 2] it is supposed to expect [2, 1]
Can you post your code, so i could use it to reproduce and fix the issue?
Reference solution was buggy and returned wrong expected result for integer=2. Should be fixed now.
very good
Rust (and probably other languages) doesn't have a test for square numbers, so plenty of solutions double count the square root.
expected '15 is a prime' to deeply equal [ 3, 5 ] what?
At some point your solution returned
"15 is a prime"
, while it should return[ 3, 5 ]
.コノシート・
Failed asserting that true is identical to Array &0 (
What the hell does this supposed to mean?
Although, I completed this task.
Hi! im having this error msg and I dont undestand why : Failed asserting that null is identical to Array &0
My array has the correct numbers in the correct order
This comment has been hidden.
Please see if this paragraph and this paragraph help.
Your solution is not exactly correct. Your problem is not caused by a kata issue.
thx, I realized my mistake
Please review: C++ Translation
This comment has been hidden.
It was a good one!
C:
srand
should not be called intester
, otherwise it re-seeds RNG with every test case and causes repetitive inputs.i rewrote the tester function
Seems like tests for C implementation crashing in random places with error 'Caught unexpected signal: SIGFPE (8). Erroneous arithmetic operation.'. I tried to catch problem n-values (thought it's my algorithm was incorrect), but seems like tests crashing before executing my realisation of divisors() directly.
In process I also discovered that generated n-values for tests repeating very often, in succession or periodicaly, so maybe random generator also broken for C implementation.
I raised an issue specific to incorrect setup of the random generator.
Still, tests in C do not crash for me and they work (more or less) as expected. I'd tend towards suggesting to look for the problem in your code, or posting it here so we could check it.
SIGFPE
fixed + RNG fixedmy code takes less memory space than expected. so I'm supposed to add variables just to saturate?
Hi. When asking for help, you need to be more specific: which language are you using? What is exactly your problem? What are you doing, and what is going wrong? Provide the exact logs that you are receiving.
You can post code, but if you do it don't forget to put on the spoiler flag on your message and to format your code correctly.
For more details and informations, you can refer to the official documentation: https://docs.codewars.com/training/troubleshooting/
C: tests verify the correctness of
*z
, but then use the potentially incorrect value to compose the assertion message, effectively leading to a crash.fixed
Hi everyone! Please help me understand exactly what to do in this task, I want to fill an array with the divisors of the number n, but I get the following error: "Caught unexpected signal: SIGSEGV(11). Invalid memory access." As a test, I tried to remove the work with the array altogether, but I still get the same error. WTF?!
There's many possible reasons for SIGSEGV (https://docs.codewars.com/training/troubleshooting#c-c-signal-11---sigsegv), but it's difficult to tell what happens in your case without seeing the code.
Most probably the reason for your failure is
*z
not being set to a correct value, but still tests should not crash.This comment has been hidden.
My code fits the outputs but still says that i have not given the correct output
Not a kata issue. You need to return the value, not print.
This comment has been hidden.
Asking for help is not a kata issue !
For one, remove the line with
prompt
and replaceinteger
withn
. We don't work with console inputs on CodeWars. Secondly, if number isn't prime, you are not returning anything, only logging something to console (which is only for debugging).Feel free to reply if you're still stuck.
COBOL translation.
Approved
It will be better if the method returns an empty array instead of string.
Yes. Unfortunately this is an old kata and this won't be changed now for already translated languages.
good evening everyone.
I have a problem... in my Visual Studio Code, i have good response... but in Code Wars i have: "expected undefined to deeply equal [ 3, 5 ]" i don't see the problem.... i return an array with good divisors Thanks for your help, i'm a novice in Code Wars and development ^^... and english (sorry)
Your function probably returns nothing. You need to
return
the result, not print it to the console.Great ! Thanks a lot! ^^ Thanks to you I made this kata.
my answer didnt work because of *z must be set to 0 at start dont think its good practise
It is not a good practice to return a null value (or string instead of an array). It causes the
if...else
statement in our method and then the client also have to check if it is null or array. It will be better if the method returns an empty array when there are no dividers.I agree
This comment has been hidden.
With 13, your if condition isn't true and you return an empty array instead.
C Translation kumited (author inactive).
approved
Please see this issue
I like it; it was very exiting I am proud of myself!
This comment has been hidden.
I embarrassingly had a tough time with this. I've not spent much time or had any formal math or algorithm courses. That said this is incredibly frustrating and poorly explained writ large across the internet. I feel inadquate and disgusted.
Saame fam
This is a really good example of a challenge that needs to be thoroughly commented. Imagine trying to work with all these different answers and figure out what is going on! Comment your code people lol
This comment has been hidden.
Print the input, see why your code fails with that value.
i'm sorry, maybe i'm stupid, maybe its all my bad english, but i don't understand. i guess this error comes when the answer must be string (like "13 is prime"), but idk why it want array type in this case
Put this as the first line of your function:
echo($integer."\n");
The answer should be a string, your code is returning an array instead.it explicitly says that it should return a sorted array in ascending order but when i attempt i get this message
"The array is not what was expected: expected [ 12, 2, 3, 4, 6, 8 ] to deeply equal [ 2, 3, 4, 6, 8, 12 ]"
Isn't it quite clear that your result not sorted at all?
Suggestion: If no divisors are found, return an empty array, just to keep type consistency in the returned value! ☺
This is a bad suggestion as this question is aimed at functional programming with algebraic datatypes. In Rust the actual return types for this function are Result::Ok(u32) and Result::Err(String). Result is a useful monoid that makes error handling super simple when you combine it with pattern matching.
Too late to apply such change as well
This comment has been hidden.
Not a kata issue and please don't post solutions in Discourse.
Drops timeout while MsVS 2019 return result in less then 60 ms :\
A problem with your code is not a kata issue:
Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
I don't think that one FOR cycle can ruin servers, while Studio doesn't drops any loops or errors
That depends on the amount of tests and the input values. There were some heavy performance tests only in C# version, I commented them out. Try again.
Hi folks, I have written my function and tested with success in IDLE but in the codewars system I have the same issue as codekk with the following messages: None should equal '13 is prime' None should equal '13 is prime' None should equal '29 is prime' The other test has paased. What's wrong in my code, please? Thanks for your advices.
Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
This comment has been hidden.
Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
This comment has been hidden.
This comment has been hidden.
The integer 1 is technically a divisor of every number.
This comment has been hidden.
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
This comment has been hidden.
You're concatenating lists that way.
Hello, I think I encountered an issue. This is for Python, My Test Results output is the same exact results for the test cases, but it failed all 3 test cases:
Test Results: Log [3, 5] None should equal [3, 5] Log [2, 3, 4, 6] None should equal [2, 3, 4, 6] Log 13 is prime None should equal '13 is prime'
Am i missing something?
Yes,
return
Expected: null But was: empty and? I dont get it... return null; but how can i return string "null" if I use int[] type? C#
This comment has been hidden.
This comment has been hidden.
potentialDivisor <= n / 2
is too much. Upper bound of your loop can be much, much much lower.This comment has been hidden.
You returned a tuple, but in this case you should return a string, for example
"13 is prime"
.Thank I used return "{} is prime".format(integer) and got it right.
The test throws an error "Array (...) does not match expected type "string"."", But my code already returns a string when needed.(language - php)
Dart Translation Translation Added! :-)
Approved
This comment has been hidden.
This comment has been hidden.
(13, 'is prime') should equal '13 is prime' getting this error and not accepting the solution
return(integer,"is a prime") This is what I coded
Do they seem equal to you? The result should be a string, not a tuple. Issue with your code is NOT a kata issue, so, closing...
Rather than returning a string if a number is prime, it should just return an empty array (mixed return types is bad practice)
Edit: but making this change would invalidate tons of solutions
This comment has been hidden.
Your global var keep values between tests when the function is called more than once, make it local.
This was a very good question, I had written an approx 25 line code for this, when I saw others' solutions which were completed in just 2 lines,I lost my shit!
This comment has been hidden.
For C#, kata runs 1000 tests with not that big numbers (up to 100_000).
This comment has been hidden.
It's not formatted in a readable way.
Things to read: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#post-discourse
Read the description again, it says return, not print.
This comment has been hidden.
Please read last bullet point of this FAQ paragraph: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#when-i-print-my-answer-it-looks-exactly-the-same-as-the-expected-output-yet-tests-fail
This comment has been hidden.
Then why didn't you use
Suggestion
instead?It's already there:
If you meant the description should have the definition of what a prime number is, it assumes you know that (or you can google it).
Don't give up!╰(°▽°)╯
The objective of the test was really good! But I think the solution should check the constraints as well. Just an opinion :)
This comment has been hidden.
Spoiler flag, please.
Anyway, you need to remove your own tests from your submission.
Also, you need to make sure that you have 3.6.0 selected since you use the f-strings which is 3.6+ only.
After doing that, your solution works fine.
Oh boy, I didn't notice I had to pick the newest Python version .. yes now it worked thx!
And regarding the spoiler I did click in the flag, apparently somehow it was not set? well weird..
One of the unusual things is that you have to click right in the box, maybe your mouse accidentally fell on the label.
This comment has been hidden.
You really need to optimise your code to be able to pass this in C#. I wouldn't say it's tremendously tricky to do, but it does require some thought.
But I agree that, as far as C# is concerned, this is more difficult than 7 kyu.
This comment has been hidden.
The past 2 challenges I attempted have shown failed test results. Even though my output always matches the log on test results.
Example: https://i.imgur.com/JgX3A5g.png
return the expected value instead of printing it
Thankyou
Im having the same issue. has anyone seen a solution to this?
Yes, please, read the posts (rowcased already said what was wrong and how to fix it), if you see the same output, you're also printing instead of returning the result.
This (in C#) is certainly not a 7kyu with the test to pass and its time restraints.
2 is prime (the only divisors of 2 are 1 and 2 itself), but the random test says that it should return [2, 1]
Hi quick question,
For some reason the test is showing that my code is failing to say "13 is prime." But, when I run the same code in Jupyter, it turns out just fine. Anyone have any ideas why that is?
Language? Are you returning the result or just printing it?
Python, yes I have it return a variable that I set as a string
Could you copy and paste the exact error message? I see a period there:
"13 is prime."
and the expected value is"13 is prime"
without it.This comment has been hidden.
This comment has been hidden.
There's an algorithm called Fermat's factorization method that you can look up on Wikipedia.
For elixir: The test for integer = 2 is wrong.
Correct result: "2 is prime" Result expected by Codewars: [2, 1]
i have the same issue, is there a way to fix the tests?
Hello there,
first time here on CodeWars. I was just wondering. Is errors catching also part of a good Kata solution?
It seems that I took the "Create a function named divisors/Divisors that takes an integer n > 1 " too serious and added a raise in ValueError if n <=1.
The most upvoted solutions are pretty clever and compact, but they take in input n <=1 and return cases like "-8 is prime".
Shouldnt handling errors be part of the solution too?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Your function should return the result instead of printing it.
if(len(l)==0): print( integer,"is prime")
thats what i did. im facing the same issue. lol
return( integer,"is prime")
12 secs timeout, with 30ms execution of tests, no clues as to how to optimize
My code works fine when I test in Spyder3 but it is failing with a message "return is an outside function"...since it is not that complex, looks to me this kata needs to be rewritten
This comment has been hidden.
You're not returning anything.
Your functions should return in CW, not print. Also, use Question, not Issue label for things like this, and use proper markdown
Thanks for the answers, I'll use the proper markdown next time. Sorry for the inconvenience.
This comment has been hidden.
Your functions should return in CW, not print. Also, use Question, not Issue label for things like this, mark your post as having spoiler content and use proper markdown
This comment has been hidden.
There're 1000 tests with values close to
int.MaxValue
. Your code is not performant enough.This comment has been hidden.
You should return the string.
This comment has been hidden.
This comment has been hidden.
Use only one kind of quotes when you set
str
value and choose 3.6 in version selector.This comment has been hidden.
Because your
else
is run on every iteration this way.I'm not sure how to fix this?
Timeout error in C# Execution Timed Out Execution Timed Out (12000 ms)
Run the program in VS and works fine.
I've just solved this kata and there are no problems with it. You need to make your solution a bit more performant.
Also getting the Timeout, but only on the Attempt button... 'Run Sample Tests' returns right away with the message: Completed in 26.2250ms
Even providing 2mb of tests completed without a problem.
Same here. Tests run fine, but apparently, my code takes over 12 seconds to complete when I Attempt. Using List class and converting to array when returning, which I think might be the problem.
Rum Sample Test: You have passed all of the tests! :) Attempt Execution Timed Out Execution Timed Out (12000 ms)
Python. There should be at least 100 random tests.
There also should be fixed tests where the input number is a square.
added
This comment has been hidden.
This comment has been hidden.
Not an issue. Your code is too slow.
Execution Timed Out STDERR Execution Timed Out (12000 ms)
When attempt. But when running tests - all right.
Not an issue.
This comment has been hidden.
@GiacomoSorbi could you fix this?
Fixed.
This comment has been hidden.
Hi everyone,
I find it easily to solve so I am wondering about this problem is for beginners right?
I am looking forward to hearing back from you guys
Test Results: Execution Timed Out STDERR Execution Timed Out (12000 ms)
var array = new List(); return array.ToArray();
I submit the code I receive this error: "The array is not what was expected - Expected: [3, 5], instead got: '[3, 5]'". I don't have single quotes in my code at all so I have no idea what the issue could be. Is there any way around this issue? Thank you!
Those single quotes mean you're returning a string instead of an array.
I solved it! Thank you very much!
Convert them to int inside of the array.
I submit the code I receive this error: "The array is not what was expected - Expected: [3, 5], instead got: '[3, 5]'". I don't have single quotes in my code at all so I have no idea what the issue could be. Is there any way around this issue? I'm working on the JavaScript version of the kata. Thank you!
Closing this one, it's duplicated.
This comment has been hidden.
Works fine for me. Are your indents correct?
This comment has been hidden.
Mark your post as having spoiler contents, and you're doing something inside the loop that should be done after it instead.
This comment has been hidden.
This comment has been hidden.
Rust version has no random tests. CS/JS/Python/Ruby has very weak "random" tests. And C# version is not consistent with Elixir/PHP either.
This kata really needs to be rewritten.
Python tests look okay to me, assuming they've been fixed in the last five years. Just commenting here to note that.
Fixed JS.
added random tests to Rust
This comment has been hidden.
This kata will test some very large numbers and time out. I won't spoil the answer, but you don't have to test all the numbers from 2 to n.
Instructions should specify "positive integer", or test for negative integers.
This comment has been hidden.
1 is not prime. But I had to add:
divisors 1 = Left "1 is prime"
to my code in haskell.
I think tha tshould be corrected.
https://math.stackexchange.com/questions/120/why-is-1-not-a-prime-number
Yeah. Doing that hurt a little.
I don't know how to work it out
That's the wrong question. The correct question is, it's not specified at all how
1
is handled.I've changed the input range to
n > 1
. Given that1
should be omitted it doesn't make sense for1
to be the input anyway.For number 13 my code returns "13 is prime", and it fails the test with "None should equal '13 is prime'"
Are you sure, your code is returning it and not printing it?
check if your function has that in a return statement and not print statement
You may have missed a space in your code
Problem should also indicate that result array should be in ascending order
Or use a collection equality comparer...
alr updated some time ago (
from smallest to largest
)Passing all sample tests but getting this error: TypeError: divisors(...).join is not a function at Object.handleError at ContextifyScript.Script.runInThisContext at Object.runInThisContext
Typically means you're returning a datatype that isn't amenable to having
join
called on it (i.e. not an array).My code below passes all the tests I run on my own shell. Is there a problem with the test?
Just because it works for one case on your own IDE, it doesn't mean it actually works for the whole test suite.
Please don't mark an issue if it is a problem with your code.
This comment has been hidden.
This is not an issue. Your code is too innefficient
This comment has been hidden.
This comment has been hidden.
As a mathematican, I was utterly infuriated when 2 was [1,2] instead of prime.
2 should return "2 is prime" not [1, 2] :P
Mine said 2 should return [2], not "2 is prime".
Booo.
This comment has been hidden.
This comment has been hidden.
It means your solution is too slow. Improve your algorithm :P
Writing optimized code is a fine goal, but there was no attempt to communicate the performance requirements in the Kata description. My solution meets the requirements described in this Kata and passes the basic tests, so I believe there is still a legimate issue with this Kata whether I improve my algorithm or not. I can't even view and learn from the better solutions until I meet the hidden requirement.
This comment has been hidden.
This comment has been hidden.
This isn't really a solution, it's basically a cheat. It isn't doing what you think it's doing.
Tests don't pass- order seems to matter, should be in ascending. Instructions should be updated
alr updated some time ago (
from smallest to largest
)This comment has been hidden.
This comment has been hidden.
Not an issue?
Got this output, need some help
Expected: '[3, 5]', instead got: '3'
My code passed sample test case but when I click on Attempt, it returns process terminated as it took too long. I tested my code for integer 32768 upper limit and it executed in 0.016 seconds.
Yep, this Kata sucks. I tried 3 different approaches locally and works fine while here I constantly gets timeouts.
Happened to me too. Took me a long time but I ended up realizing my code still wasn't efficient enough and fixed it.
This comment has been hidden.
what about a return statment.........?
This is because your algoritm is not efficient enought. You can't just iterate throught all numbers between 2 and passed value because you will exceed allowed execution time - tests are written in that way so you have to use basic math to find the solution.
Expected: null But was: empty (with chevrons around the word empty)
I'm writing in c# and my code is passing the tests (albeit slowly, I think it's pretty damn inefficient) except some of them, where I'm getting the above error. How do you convert an Array to null?
Arrays are nullable, just
return null
,int[] a = null
etc.PHP Translation Kumited - please carefully review and approve :D
[CoffeeScript] Random tests sometimes generate the integer input
1
and then expect"1 is prime"
which is not true.That's the wrong question. The correct question is, it's not specified at all how
1
is handled.I've changed the input range to
n > 1
. Given that1
should be omitted it doesn't make sense for1
to be the input anyway.This comment has been hidden.
The test method uses .join into the return, but the return can be a string, so it gives exception .join does not exist when the string 'x is prime' is returned by the function. (im using Javascript)
Changed to
assertDeepEquals
and removed thejoin
An admin needs to take a look at how this code is being tested in Python. My code passes any test I write for it in my text editor. Even the tests that you display on the page. This needs a serious once over.
I'm not an admin, but I've checked the tests and they look good to me.
If you would show me your code, or be a bit more specific on what you think is not working, I'd be happy to take another look at it.
Not an issue
This comment has been hidden.
I'm not the author, but I wouldn't say this is an issue. Your code has flaws and some tests don't pass. In fact they crash.
For example, for 13, your code would return an empty array instead of
"13 is a prime"
and for 105 (3*5*7
) it would say105 is a prime
, which it isn't.So, I'm removing the issue. Forgot to do it before posting. :-)
Hi Abbe,
Thanks for your reply. I had resolve the issue from another comment, my fault was when the number is 253, it return "253 is prime" instead [11,23]
Thanks Abbe :)
This comment has been hidden.
It normally means that you forgot to actually write
return
in Python. Common mistake if you come from Ruby, for example.thanks a lot
This comment has been hidden.
This has been removed from Python3 and only works in Python 2, and you are trying it in Python3. You need to change the version of Python by clicking the dropdown box next to the language and select "2.7.6". Then reattempt the testcases and you should pass.
This comment has been hidden.
Did anyone find out the solution to why the error is being thrown?
TypeError: divisors(...).join is not a function at Object.handleError at ContextifyScript.Script.runInThisContext at Object.exports.runInThisContext
I am returning an array.
I was not checking for 49 and 253. I am now passing all tests!
I'm still experiencing this.
@vohof can you post in your using Markdown formatting, see here: https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting
```javascript
code
```
Hello, I'm using JavaScript. An attempt to sumbit my final result received the following error. I see other had a same issue and resolved but the comment are hidden.
253 TypeError: divisors(...).join is not a function at Object.handleError at ContextifyScript.Script.runInThisContext at Object.exports.runInThisContext
Make sure you are returning an array since that's what the tests try to do join().
This comment has been hidden.
Not an issue, a
Hey, I keep getting the same error when attempting to submit the final result (I am returning an array). . How did you solve it?
n=2, returned [] Should fail! You can run primes from 1 to 32768 without no effort. Not just rand()
This comment has been hidden.
Fixed
I am not sure if the test cases it uses are always right.
I got an error back that said "✘ Expected: [2], instead got: "2 is prime" " -- but 2 is a prime.
I ran it again and it passed.
Not clear, and not an issue
Hi, I get the next error when I run the summit test: TypeError: divisors(...).join is not a function at Object.handleError at ContextifyScript.Script.runInThisContext at Object.exports.runInThisContext
I don´t know where this error comes from. I sorted the output array and I return the array instead print. Any suggestion?
Can you post your code as a reply to this comment, since I can't make an accurate comment unless I see your solution.
Thanks for the replay. Finally, I found the problem. Numbers of iteration was not defined base on the number.
This comment has been hidden.
Amen that, but allow me to put a spoiler flag on it ;)
Can you please explain why this works? Thanks!
This comment has been hidden.
This comment has been hidden.
It really depends on your logic. Not all ways of doing this add both divisors to the array. i.e. when getting factors of 72 and you are testing 2, not all algorithms will also add 36 in the same iteration. If you take a functional approach such as filtering a range of 2..x, x needs to be the initial input divided by 2. If you are looping and adding both divisors, then square root works fine.
If I use any other programme to run my code, it passes the test. But when I paste it here, it says:
Traceback: in NameError: name 'integer' is not defined
What can I do to make it work?
I'm assuming that you're using Python and that you are typing 'integer' without quotation marks which thinks it is a variable even though the variable has not been declared. I'd be happy to look at your code and help you if you reply to this comment.
This comment has been hidden.
return result
instead ofreturn print(result)
divisors(integer)
. It looks for a variable named integer and because it's not found, it will return an error. You can remove this statement since testcases will call the function automatically.This comment has been hidden.
It passes all tests, but when submitting it keeps saying divisors(...).join is not a function. what is this??
I write a code in python, it work fine, but it keep giving error, when I summit it for a test None should equal [3,5]... does anyone have this issue
i also have the same issue
yh i also have same issue
You need to
return
the value instead ofprinting
it. Returning a value allows it to be checked for equality whilst printing it just outputs it to the console.None
is the default value when nothing is returned.yeah,You need to return the value instead of printing it.
The return value has to be sorted smallest to largest.
For the example 15, [3,5] is right, but [5,3] is wrong.
It should specify this in the description if this is the target, but I think any order of answers should be acceptable.
Added ordering requirement in the descriptions
This comment has been hidden.
Not a kata suggestion
The test cases should include a number that is a perfect square to test those solutions that use sqrt.
The test cases expect a perfect square to have the result repeat the square root twice, such as 25 -> [5,5]. It shouldn't be repeated though, and only expect it once like 25 -> [5]
This comment has been hidden.
Maybe return instead of print your final results?
Getting "TypeError: Object 253 is prime has no method 'join'" wtf is this... and i passed the first 3 tests....
I'm having the same issue, I even put 253 in as a test case and it passed fine. I'm using JS and my code is
return integer + " is prime";
Hi, I have the same problem. Did you find the way to make this work? It´s driving me crazy!!! :D Thanks
Why you didn't use template literal?
That's because you're returning the wrong thing.
You should add a test or two for numbers that are perfect squares (4, 6, 9, 36, etc.) as an edge cases. I have a function that pass these case minus this case.
Object 253 is prime has no join method. What is wrong here? I ran all of the tests myself and they all worked normally, but when I submit, I get the error. Please help.
Same here -_-
s/"The array is not what was expected"/"divisors(15) should be 3,5"
repeat for all test case error messages.
Thanks--that was a fun kata!
Description says :
. . . your code
else { return "[" + array.join() +"]";}
returns an "array-looking-but-not-an-array"-string ! (and strings don't havejoin
method)I would suggest having a perfect square number as one of the test cases during final submission, because I was able to create a solution that passed all of the cases, but fails for primes (it duplicates the square root in the output)
Hello, I came up with an error when I go to submit my code: TypeError: Object 253 is prime has no method 'join' but when I pass that in a testcase I figure out that I have the correct output. What is wrong? Am I suppose to change something to return value? var str = integer + ' is prime'; return str; Thanks in advance.
Hi, can you post more information? Like your attempted solution and the language in which the error came up. Thanks
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Hi, I see that your issue was solved, but the comments are hidden, because I have not solved it yet. But--I do pass all input tests, I'm just getting the same error as you involving Object and join, which I did not use in the function. So, how did you resolve it? Thanks!
A much faster way to calculate all divisors is to get the prime factors and find all combinations of the prime factors, then strain out the false positives. If you wanted to find all factors of 1 - 1000000 for instance, that would take several minutes the naieve way.
This kata requests a function that returns two different types of return values. This is BAD PRACTICE. This is a fine test of your programming skill, but it's a bad habit and promotes worst practices for writing re/usable code.
If you are trying to learn how to write code (tactics) this is an OK exercise. If you are trying to learn how to write reusable code effectively (strategy) this is a POOR exercise.
Have fun, just be aware! (I'm not sure if the "Issue" tag is appropriate; just let me know)
That's one of the main problems I had with this Kata. But as you said, it's not bad if you're exercising your programming skill (as I am).
This comment has been hidden.
As a matter of fact, you don't need to analyse beyond the square root of the given integer. In that case, just add the result of the division as well (if it is not the same, at least). I would propose to test quite large numbers as well, in order to demand more efficient code for large numbers.
I thought there will be much bigger numbers in hidden tests, I expected this to be much more difficult, but I tried my luck and submitted solution that works just for small numbers and I passed
Maybe a bit too easy as a kata, but good for some lightweight practice.
And you just got it translated into Ruby :)
This comment has been hidden.
Yep, 1 is definitely not prime. I was slightly surprised by that being ommited from the spec, but it was not included in the test cases for JavaScript.
That's the wrong question. The correct question is, it's not specified at all how
1
is handled.I've changed the input range to
n > 1
. Given that1
should be omitted it doesn't make sense for1
to be the input anyway.fyi:
I see a lot of people check for
len([]) == 0
in Python solutions.An empty list is a falsy value in Python, so
if len([]) == 0
is equivalent to plainif []
, but the later is more Pythonic.According to the current spec, divisors(1) should return "1 is prime". And there's no test for it, too. Maybe you want to look into that.
I am doing this kata tonight. I am thinking about the same delemma - how to treat 1 because by the United State's defintion, the number 1 is not a prime number. Since it's not a prime number, and the problem specifically asks for returning divisors other than 1 and itself. So should I just return an empty list if the input is 1? Or is the author of this kata thinks that 1 is a prime. In some other countries, 1 is treated as a prime number (FYI).
Enjoyed the Kata. My first one on the site. Thanks!
Thank you!
Thank you for the kata. I think the second test has a typo. The error says can't find divusors.
Thanks! It did have a typo, thank you for pointing it out, I fixed it.
O(sqrt(n))
The description of the kata is all messed up.
All fixed. Marked as resolved.
Should have test cases written
All languages have example test cases now.
formatting is an big issue, no example test cases!!
All languages have example test cases now.
Great kata. Well done.
Thank you! Really appreciate it.