| Filiprei (132) | |
| This program has been running for quite some time now! I am looking for the first triangle number that has over 500 factors. I hope I haven't waited all this time for nothing! | |
|
|
|
| Volatile Pulse (1329) | |
|
I assume you're working on one of the Project Euler problems. I'd end the program if you waited for more than 15 minutes. I personally only wait 5, but to each their own. You need to test it with small numbers first just to make sure you're not running an infinite loop/recursion. Another thing to check is to make sure you don't go out of bounds, but I don't believe there is an issue with that. | |
|
|
|
| Filiprei (132) | |||
Stupid me I forgot the code:
What makes this work so slow! and does it work at all? | |||
|
|
|||
| Volatile Pulse (1329) | |
|
You're bruteforcing it. It's going to take a while to eventually get to the end, but you would. I wouldn't suggest waiting for it to find the right answer though. I'd reevaluate your algorithm. Edit: On second thought, I don't know that it would eventually get you there. Let me find what I used...if I ever did that one -.- Edit: I also used a brute force technique. I didn't write down the time, but mine is faster since it doesn't output every triangle and their factors. I'm still running your program after several minutes and still no answer. I believe I let mine run for about 30 mins on my computer when I finally got the answer. | |
|
Last edited on
|
|
| Filiprei (132) | |
| What algorythm would you use? I have waited for 4 hours now but I just cancelled because I understood it wouldn't get high enough :/ I need some help with getting a more effective algorythm. | |
|
Last edited on
|
|
| cire (1847) | |
|
I implemented a sieve to solve that one. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Sloppy code for my solution, in case you get stumped and want to cheat: https://gist.github.com/4223571 The sieve implementation isn't very clean. I'm pretty sure I had it in mind to make the sieve's progress saveable and resumable, which partially explains the crappy design, but it's sort of readable. | |
|
|
|
| Filiprei (132) | |
| Isn't that formula to find prime numbers not triangle numbers... | |
|
|
|
| cire (1847) | |
|
Yes, it is. The problem is only tangentially about finding triangle numbers. Mostly it's about finding the number of divisors. To find the number of divisors, it helps to know which primes the number is evenly divisible by. If you're going to brute force it, you should at least cache the primes as you calculate so you aren't calculating the same primes over and over and over again. | |
|
|
|
| Filiprei (132) | |
|
Does odd numbers in general have less factors? because then you could just not calculate any odd numbers... :/ Im not sure though, but even numbers can be divided by some odd numbers and some odd numbers, while odd numbers can never be divided by even numbers :) (even with that prime thing you have I don't think it would speed it up enough to do it in under 10 min. Only a little partion of the numbers are primes. I think I would have to change the formula totally! Its stupid that they didn't atleast ask for something that don't take ages to find out. Example: first triangular number with 200 factors, my computer would be able to handle that! | |
|
Last edited on
|
|
| Filiprei (132) | |
|
Ok just give me a triangular number not too far below the answer to the problem(like 10 triangle numbers below, if you understand what I mean, without telling me the answer) so that I can check that the program works. Thank you so much, I hope you continue to help me with my project euler problems, I am only 14 years so every time I do a problem I get really happy:P | |
|
Last edited on
|
|
| cire (1847) | ||
It doesn't take ages to calculate. In fact, using the sieve and a little knowledge, it takes under a second for my computer to calculate the answer. http://www.wikihow.com/Determine-the-Number-of-Divisors-of-an-Integer | ||
|
|
||
| Filiprei (132) | |||
Ok you are probably right but if we just focus on this part:
I don't see how you could use this: http://www.wikihow.com/Determine-the-Number-of-Divisors-of-an-Integer to make it any faster. Unless ofcourse finding out the prime factors are faster. I don't see how you could implement it though, please explain. | |||
|
|
|||
| cire (1847) | ||||
| ||||
|
|
||||
| cire (1847) | ||||
Contrast that with the brute force approach. Note that I made one small optimization from your implementation. I reduced the number of times the for loop in your number_of_factor function by about half, so consider how much larger the numbers might be:
[Edit: Oops. Small mistake. That should've been division, not multiplication. Have it fixed in a sec. Fixed!] | ||||
|
Last edited on
|
||||
| Filiprei (132) | |||
I tested it but the number of divisors is really low, I think they are lower than they should be, know why? I tried your program it worked fine this is my code:
| |||
|
|
|||
| cire (1847) | |
| You're going to need a lot more primes. | |
|
|
|
| Filiprei (132) | |
| Is the primes used for prime factorisation? How can I find primes in a good way? | |
|
|
|
| cire (1847) | ||
I gave you a link that explains how the primes are used. I gave you code that showed you how the primes are used. I gave you a link to code that shows you one way to find primes (and solve the problem in question,) although in this case a brute force approach is doable. Maybe you should actually piece it all together and quit asking questions you already have answers to. Use google. Write code. | ||
|
Last edited on
|
||
| Filiprei (132) | |||
This is my code now:
I am not sure if it works, and if this is enough primes. | |||
|
Last edited on
|
|||