Okay I feel like a really big noob I am trying to solve this problem by factoring like this
100
/\
50 2
/\
25 2
/\
5 5
But the problem is I don't even know what the first value that can be factored in is... My code is compiling super slow could anyone possibly help me write up some code that will run much faster. Here is what I have so far ( still waiting on compile could be a VERY long time.
Yeah the vector was just for me so I could see what the factors were and whoops I had auto i = 3LL; but I must of accidentally changed it. Thanks for the response is there any way to make it compile faster though or do I just have to wait it out for an answer? I am working on the other problems now since compiling will take a few hours I am guessing.
Compiling should take a few seconds at most. That's the process of turning the source code into machine language which the computer can understand.
After that you can execute (run) the resulting program. If it takes more than a few seconds it probably means: either you have a very inefficient algorithm or there is an error in the code causing it to get stuck in an infinite loop.
I have a very inefficient algorithm sorry I meant run the program not compile. That's where I was looking for help on improving. Some of these problems are challenging haha I have finished problems 1 2 and 4 so far. 3 is taking too long to compile and 5 I think I did something wrong and need to fix. These problems are pretty fun though and great practice.
*edit*
Ps should I make a new thread for my question on number 5?
----
I am having a difficult time I tried dividing the number by all the primes less than 20( 19 , 17 , 13, 11 , 7 , 5 , 3 & 2 ) then increment but the answer is always just those numbers multiplied which clearly is not the answer.
#include <iostream>
int main()
{
auto number( 2508uLL ); //closest number to 2520 that is divisible by 19
constunsignedshort primes[ 7 ]{ 17 , 13 , 11 , 7 , 5 , 3 , 2 }; //didn't include 19 because it's a given
bool divisible( false );
while( !divisible )
{
number += 19; //the number must be divisible by the highest prime..
divisible = true;
for( constauto &it : primes )
{
if( number % it != 0 )
{
divisible = false;
break;
}
}
}
std::cout << number << std::endl;
}
I have come up with a way to find primes so by doing a about 1000 at a time to start then doing about 10k + after that not too much at a time though or it will take forever to compile it took me about 2 minutes to compile the first 100k primes using this:
I ofstreamed the first 8 primes to start 2 - > 19 and the reason I have the sort/unique is because there is a slight bug where it repeats each time you run and I was too lazy to figure where the bug was. Thanks for all the help.
**Edit
Literally took two seconds to get the answer after I read in the prime numbers from that text file. =]
Oh I see now and I must have really messed up earlier because that was what I was trying to do. I just re-wrote it after looking at wiki and it works similar to tnt's but different code. I don't like to copy and paste. Thanks for the suggestion
Keep in mind that the runtime of program algorithms is quantifiable, and you can learn about it in most data structures or advanced data structures-like courses or text. I see that in your most recent implementation you dropped the recursive calls of factorize(), which would definitely improve the runtime.
For-loops, and while-loops as well, are very resource intensive, especially for-loops that are nested. Add even one recursive step to that and a calculation gets incredibly more complex. Certain data structures can be much more efficient even with recursion, such as a binary tree, or binomial heap (both with a runtime on the order of log_2(n) or something similar, where n is the number of nodes).
Just my 2 cents before bed, sorry that I can't contribute to a code-related question.
oh it's okay I did that sieve method and it ran in .2 seconds and number 10 in 1.2 seconds which is pretty good I think. Also yeah I am not even in a programming class I am just read up on things in my free time and trying out new things. Trying to finish all those project euler things they are interesting. I haven't really got around to binary trees or binomial heaps or even nodes.