-1

I have another backtracking challenge, in which I have to get all possible combinations of prime numbers that add up to a certain number. I have finished the task using the general use algorithm from Wikipedia, but for the number 100, it took more than an hour to run, and it still hadn't finished by the end of class. I was wondering: Would memoisation(how do you spell that?) have significantly improved the algorithm's performance(as in, would it have made it noticeably faster)? I am using c++, and the function is called a huge number of times. I am using recursive backtracking, which I seem to remember is roughly O(n!) for simple problems.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
Mark Gardner
  • 442
  • 1
  • 6
  • 18
  • What have YOU done for the performance evaluation or checking? You must put here your own starts and results – Gangnus Jan 19 '18 at 07:52
  • I don't really know how... I'm using a school computer, with windows 10 and code::blocks 13.10. – Mark Gardner Jan 19 '18 at 07:58
  • In the current form, your question is forbidden. Somebody had already voted for closing... You can ask for advice and even help when you have problems in your solutions, but you can't ask us to do your work. It would be a bad service to you, too.... You should write here how you are counting the complexity of the algorithm and at which point you have stucked. Or how you are measuring the real complexity of the program. And put the code here. Both ways are OK.But you have to do something yourself FOR THE TASK. Buying a computer and OS/frameworks installation is NOT enough. – Gangnus Jan 19 '18 at 09:00
  • @Gangnus I meant that I do not have simple unix utilities like `time` available (as far as I know), and don't know if the IDE has any way of doing it. – Mark Gardner Jan 19 '18 at 16:02
  • There is no way for us to tell how to improve a program that only you know how it looks like. Some people around here are very good programmers, but even here, no one is psychic. – nvoigt Jan 19 '18 at 16:03
  • @nvoigt I am calling a function that I have provided a link to O(n!) or so times. Would saving some answers (such as 11) significantly reduce computing time? – Mark Gardner Jan 19 '18 at 16:08
  • @MarkGardner Yes. Is that the big answer you are looking for? Your answer is "Yes". Putting things in a cache instead of calculating them again is most likely going to get you speed improvements. – nvoigt Jan 19 '18 at 16:09
  • @nvoigt but will it be noticeable? – Mark Gardner Jan 19 '18 at 16:11
  • Do you realize that we cannot answer your questions? It *will* be noticeable if you include a 3D picture of a clown... was that helpful for an answer? You need to ask better questions if you want *helpful* answers. Include your code, include your measurements, ask a *specific* question. – nvoigt Jan 19 '18 at 16:12
  • @nvoigt yes, I realise it. – Mark Gardner Jan 19 '18 at 16:15

1 Answers1

1

Create an array external to the function checking for primarity and reachable from it. Global or static, depending on the used language. That array will content all found primary numbers.

If the number in question is in the array, return true. 
if number is less or equal than squared max number in the array, return false.
Check for divisibility for all known primaries
if the number is primary, write it into array and return true
return false

That adding is simple enough. Do it and check the changed time.

Gangnus
  • 24,044
  • 16
  • 90
  • 149