Problem 179
Forum rules
As your posts will be visible to the general public you are requested to be thoughtful in not posting anything that might explicitly give away how to solve a particular problem.
This forum is NOT meant to discuss solution methods for a problem.
See also the topics:
Don't post any spoilers
Comments, questions and clarifications about PE problems.
As your posts will be visible to the general public you are requested to be thoughtful in not posting anything that might explicitly give away how to solve a particular problem.
This forum is NOT meant to discuss solution methods for a problem.
In particular don't post any code fragments or results.
Don't start begging others to give partial answers to problems
Don't ask for hints how to solve a problem
Don't start a new topic for a problem if there already exists one
Don't start begging others to give partial answers to problems
Don't ask for hints how to solve a problem
Don't start a new topic for a problem if there already exists one
See also the topics:
Don't post any spoilers
Comments, questions and clarifications about PE problems.
- hk
- Administrator
- Posts: 12832
- Joined: Sun Mar 26, 2006 10:34 am
- Location: Haren, Netherlands
Re: Problem 179
You count 93 because 93 and 93+1 have 4 divisors
You count 94 because 94 and 94+1 have 4 divisors
So these three count for 2 pairs and not 3 or 4.
You count 94 because 94 and 94+1 have 4 divisors
So these three count for 2 pairs and not 3 or 4.

War ruins the life and health of untold numbers of innocent children.
- Oliver1978
- Posts: 166
- Joined: Sat Nov 22, 2014 9:13 pm
- Location: Erfurt, Germany
Re: Problem 179
I'm a little lost with this.
This is what I get for n < 100; exactly 18 elements. For n < 1000 I get 126. According to some previous post that number is way off. Could anybody confirm this?
Code: Select all
2, 3
14, 15
15, 16
21, 22
26, 27
33, 34
34, 35
38, 39
44, 45
57, 58
63, 64
75, 76
81, 82
85, 86
86, 87
93, 94
94, 95
98, 99
49.157.5694.1125
- mpiotte
- Administrator
- Posts: 1961
- Joined: Tue May 08, 2012 5:40 pm
- Location: Montréal, Canada
Re: Problem 179
15 has four divisors: 1, 3, 5, 15leghorn wrote:I'm a little lost with this.
This is what I get for n < 100; exactly 18 elements. For n < 1000 I get 126. According to some previous post that number is way off. Could anybody confirm this?Code: Select all
... 15, 16 ...
16 has five divisors: 1, 2, 4, 8, 16
Some of your pairs are incorrect.

- Oliver1978
- Posts: 166
- Joined: Sat Nov 22, 2014 9:13 pm
- Location: Erfurt, Germany
Re: Problem 179
I see
; back to the drafting board...
/* edit */
I've reworked my sample and now get 15 elements...
That way it makes 118 elements for n < 1000.
/* edit - again */
That solved it. Thanks for the hint, mpiotte!
/* edit */
I've reworked my sample and now get 15 elements...
Code: Select all
2, 3
14, 15
21, 22
26, 27
33, 34
34, 35
38, 39
44, 45
57, 58
75, 76
85, 86
86, 87
93, 94
94, 95
98, 99
/* edit - again */
That solved it. Thanks for the hint, mpiotte!
49.157.5694.1125
-
crypto_rsa
- Posts: 5
- Joined: Tue Jan 20, 2015 10:38 pm
Re: Problem 179
I've been thinking about this problem for quite a long time but I haven't been able to figure out any speedups. Is there a trick to skip some values? (I am not asking what kind of trick). It seems to me that no particular property of the factorizations of n and n + 1 can be derived without actually calculating them.

- angzhiping
- Posts: 32
- Joined: Sat Aug 09, 2014 3:51 pm
- Location: Jurong East, Singapore
- Contact:
Re: Problem 179
Doesn't seem to have any trick of skipping values. Generating all divisiors for integers up till n can be easily accomplished in O(n*log(n)) time, so it is reasonably efficient to work on every integer up to 10,000,000.crypto_rsa wrote:I've been thinking about this problem for quite a long time but I haven't been able to figure out any speedups. Is there a trick to skip some values? (I am not asking what kind of trick). It seems to me that no particular property of the factorizations of n and n + 1 can be derived without actually calculating them.
You can also push the time complexity down to O(n*log(log(n))) by avoiding explicitly enumerating all divisors. I'm afraid it is next to impossible to go faster than linear time.
170825_b3d528c2bf87aaeff2a0ff93382fba94
-
crypto_rsa
- Posts: 5
- Joined: Tue Jan 20, 2015 10:38 pm
Re: Problem 179
Well almost all my solutions to previous Euler problems take advantage of some "trick" which usually significantly cuts down the number of numbers to investigate, so I was expecting something similar here. And I usually get the result in less than a second. For problem 179 it seems I need to investigate all the numbers less than 10,000,000 and so far I haven't been able to do so in less than 40 seconds. I still think I must be missing somethingangzhiping wrote:Doesn't seem to have any trick of skipping values. Generating all divisiors for integers up till n can be easily accomplished in O(n*log(n)) time, so it is reasonably efficient to work on every integer up to 10,000,000.
You can also push the time complexity down to O(n*log(log(n))) by avoiding explicitly enumerating all divisors. I'm afraid it is next to impossible to go faster than linear time.

-
MHealy
- Posts: 40
- Joined: Sat Nov 17, 2012 11:32 pm
Re: Problem 179
I'm not particularly well-versed in complexity, so I'm not sure if I'm repeating what the above the poster has said. However, I think it is fair to say that there is a "trick" for this question which, although not cutting down the number of possible candidates, significantly reduces the run time in another way. I won't go into more detail as it would spoil the problem; it suffices to say that it's a fairly common method to solve such problems.crypto_rsa wrote:Well almost all my solutions to previous Euler problems take advantage of some "trick" which usually significantly cuts down the number of numbers to investigate, so I was expecting something similar here. And I usually get the result in less than a second. For problem 179 it seems I need to investigate all the numbers less than 10,000,000 and so far I haven't been able to do so in less than 40 seconds. I still think I must be missing something
I just had a look at my own solution to this problem - it didn't take advantage of this, and hence, it took a very long time to run. I'm not sure how long (10 minutes, at least), but it was more than enough time for me to write two new solutions from scratch, based around the same "trick", each of which completes in well less than a second.
I think I am misreading the last part of your post, so: are you saying you can solve it, just in more than 40 seconds? Or that you are terminating your programs after 40 seconds, without the solution? If the former, perhaps check the private forum for this problem (they are always invaluable, and a significant reason why I can now solve this kind of problem much more efficiently).

-
crypto_rsa
- Posts: 5
- Joined: Tue Jan 20, 2015 10:38 pm
Re: Problem 179
My solution actually terminates in 40 seconds. I suppose it gives the correct answer but I have not yet posted it precisely because I was trying to find a faster algorithm in the first placeMHealy wrote:I think I am misreading the last part of your post, so: are you saying you can solve it, just in more than 40 seconds? Or that you are terminating your programs after 40 seconds, without the solution? If the former, perhaps check the private forum for this problem (they are always invaluable, and a significant reason why I can now solve this kind of problem much more efficiently).

- angzhiping
- Posts: 32
- Joined: Sat Aug 09, 2014 3:51 pm
- Location: Jurong East, Singapore
- Contact:
Re: Problem 179
By using the O(n*log(n)) method of explicitly counting divisors for every integer up to 10^7-1, it takes 1.8 secs on C++. Don't think there is a need to use any special kind of secret computational sauce.crypto_rsa wrote:My solution actually terminates in 40 seconds. I suppose it gives the correct answer but I have not yet posted it precisely because I was trying to find a faster algorithm in the first placeBut I guess I will have to do it and then check the private forum to see what the "trick" is.
170825_b3d528c2bf87aaeff2a0ff93382fba94
-
crypto_rsa
- Posts: 5
- Joined: Tue Jan 20, 2015 10:38 pm
Re: Problem 179
Yes, I've already learned that. I ran my code (enhanced with a cache, so that it actually runs in 5 seconds), posted the result and got into the problem forum where I - much to my dismay - learned that this problem indeed does not require any deeper mathematical insight and that the fastest algorithms which solve it are, frankly, quite naive. Fortunately, it's an exception rather than the rule among the problems I've solved so far.angzhiping wrote:By using the O(n*log(n)) method of explicitly counting divisors for every integer up to 10^7-1, it takes 1.8 secs on C++. Don't think there is a need to use any special kind of secret computational sauce.

- angzhiping
- Posts: 32
- Joined: Sat Aug 09, 2014 3:51 pm
- Location: Jurong East, Singapore
- Contact:
Re: Problem 179
Is your technique sublinear?MHealy wrote:I'm not particularly well-versed in complexity, so I'm not sure if I'm repeating what the above the poster has said. However, I think it is fair to say that there is a "trick" for this question which, although not cutting down the number of possible candidates, significantly reduces the run time in another way. I won't go into more detail as it would spoil the problem; it suffices to say that it's a fairly common method to solve such problems.
170825_b3d528c2bf87aaeff2a0ff93382fba94
-
MHealy
- Posts: 40
- Joined: Sat Nov 17, 2012 11:32 pm
Re: Problem 179
My technique is almost identical to Georg's post on the first page of the private board (which it amazes me too as long as he says back in 2008 - it runs in about 500ms in my laptop from 2011). I'm afraid I don't know exactly what sublinear means in a programming context, but I'm fairly sure the answer is sadly not. The limit in this problem just happens to be low enough that it's very fast.angzhiping wrote:Is your technique sublinear?
Sorry to hear this. I think the method used is interesting, if not interesting mathematically. I replied to your post in the private forum to address a particular issue you raised, though.crypto_rsa wrote:I ran my code (enhanced with a cache, so that it actually runs in 5 seconds), posted the result and got into the problem forum where I - much to my dismay - learned that this problem indeed does not require any deeper mathematical insight and that the fastest algorithms which solve it are, frankly, quite naive.

-
crypto_rsa
- Posts: 5
- Joined: Tue Jan 20, 2015 10:38 pm
Re: Problem 179
Thanks for your insightful and interesting post!MHealy wrote:Sorry to hear this. I think the method used is interesting, if not interesting mathematically. I replied to your post in the private forum to address a particular issue you raised, though.

- angzhiping
- Posts: 32
- Joined: Sat Aug 09, 2014 3:51 pm
- Location: Jurong East, Singapore
- Contact:
Re: Problem 179
An algorithm runs in sublinear time if it inspects a far smaller subset of the problem size. For example, binary search on a sorted array of $N$ elements inspects at most $\log_{2}(N)$ entries, and therefore runs in sublinear time.MHealy wrote:I'm afraid I don't know exactly what sublinear means in a programming context, but I'm fairly sure the answer is sadly not. The limit in this problem just happens to be low enough that it's very fast.angzhiping wrote:Is your technique sublinear?
170825_b3d528c2bf87aaeff2a0ff93382fba94
-
ashishn42
- Posts: 4
- Joined: Fri Mar 27, 2015 6:37 pm
Re: Problem 179
I wrote this code based on Sieve of Eratosthenes,which gives the answer <snip>.The answer is not getting accepted though. Can someone please look into what I am doing wrong ?
This is the link to my code..
<snip>
EDIT: result and code removed by moderator
This is the link to my code..
<snip>
EDIT: result and code removed by moderator
- Georg
- Posts: 157
- Joined: Mon Jan 21, 2008 7:00 am
- Location: Mannheim, Germany
- Contact:
-
ashishn42
- Posts: 4
- Joined: Fri Mar 27, 2015 6:37 pm
Re: Problem 179
But that line is simply cleaning up the contribution of previous prime powers and adding the new contribution. 
- Georg
- Posts: 157
- Joined: Mon Jan 21, 2008 7:00 am
- Location: Mannheim, Germany
- Contact:
