#prime_factors
1 messages · Page 1 of 1 (latest)
- Please use codebooks for your code and for the test output.
- What does this code do for an input like 35 (5*7)? Can you think of a way to compute the factors for any value?
Increase your chance of getting help and look like a pro by sharing codeblocks not images. For example, you can type the following. Note, the ``` must be on their own line.
```
for number in range(10):
total += number;
```
Discord will render that as so:
for number in range(10):
total += number;
Click here to learn more about codeblocks: https://exercism.org/docs/community/being-a-good-community-member/writing-support-requests and http://bit.ly/howto-ask
def factors(value):
factor = []
if value == 1:
factor.append(1)
for i in range(2,value+1):
while value % i == 0:
factor.append(i)
value = value//i
return factor
i have used anouther method which is correct with compiller but when i run test it is too slow in exercism
You need backticks ` for codeblocks, not quotes '
Does your for loop need to always go all the way to value + 1?
What if value is 2 ^ 1024?
as if ther number is like too large 123456 then it has only one factor that is itself
But what if it's not?
I don't think you understand correctly what's Isaac is saying here.
He was trying to guide you toward reducing the search space.
You made the comment that
if ther number is like too large 123456 then it has only one factor that is itself
which is not always true, questioned by Isaac.
Posting the test case (please use codeblock and not picture) just further proving his point, that very big number there clearly has 11 as another factor and not just itself.
def factors(value):
factor = []
if value == 1:
factor.append(1)
for i in range(2,1000):
while value % i == 0:
factor.append(i)
value = value//i
return factor
so here i am reducing the search space which is getting true for 10 test cases but still for very large number
it is still giving error
Yeah, you are technically reducing the search space but not in the correct way
Let take the number 100 for example
would you mind list out all the possible factors of it?
1,2,4,5,10,20,25,50,100
right. did you notice something about factors after 10?
it rise almost twice times
let me be a bit more specific, what are the relationship between factors before 10 and after 10
don't able to decode
1 * 100 = 100
2 * 50 = 100
see the pattern?
im not sure what that formula is about
but you can observe the same thing here
now look at factors of 36
1,2,3,4,6,9,12,18,36
so i should decrese the search like sqaure root of value
correct
def factors(value):
factor = []
if value == 1:
return factor
for i in range(2,3*int(value**0.5)):
while value % i == 0:
factor.append(i)
value = value//i
return factor
this code runs perfecly
but the number is some large that if i only do that 1*int(value**0.5) it still gave error
this complexity is the theta of root n and the previous one was the theta of n^2 that's why there is so much difference in time. Am I correct??
no idea. im not even sure what problem is this, i just wanted to chime in about the search space
the title doesnt seems to give a lot of infomation about which exercise this is
prime_factors
Once you get to sqrt(val), do you need to search any more numbers?
Yes. O(sqrt(n)) (or theta(sqrt(n))) is much smaller than O(n) (or theta(n)). Consider if n is 1,000,000. One requires 1 thousand tests. The other requires 1 million tests. If each operation is 0.1 seconds, that's the difference between 1.5 minutes and over a day.