ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Works for me too, I don't know what's the problem with it on Spoj. I'm going to dig in some SIGABRT mans, maybe I'll find some reason.
SIGABRT is generally sent intentionally via abort from C (e.g. from libc) when the programmer decides a certain set of circumstances make it unreasonable to continue. It's like an exception, but abort will unset any signal handlers so it will always end the process. You should run it with gdb to see where the program crashes. You might also set signal(SIGABRT, SIG_IGN); and see if you still get SIGABRT.
I was also thinking today it would be interesting to modify the problem slightly so that you output the numbers with a prime number of unique primes in their respective prime factorizations. I'm not sure if there are any number-theory theories related to that, but it would only be slightly more difficult. I might try it just because it will bother me if I don't.
Kevin Barry
PS It might seem arbitrary to find all primes within a certain range of huge numbers; however, finding huge primes is the basis for public key cryptography.
...
I was also thinking today it would be interesting to modify the problem slightly so that you output the numbers with a prime number of unique primes in their respective prime factorizations. I'm not sure if there are any number-theory theories related to that,
...
Hello Kevin,
this may be very interesting, but I don't seem to understand what you mean. Could you please explain this a little more elaborate, I'm very curious when it comes to number-theory.
Markus
Last edited by markush; 07-15-2011 at 04:06 PM.
Reason: typo
this may be very interesting, but I don't seem to understand what you mean. Could you please explain this a little more elaborate, I'm very curious when it comes to number-theory.
Markus
For example:
6=2*3; 2 unique prime factors
12=2^2*3; 2 unique prime factors
210=2*3*5*7; 4 unique prime factors
2=2; 1 prime factor
In the above examples, 6 and 12 would meet the criteria, whereas 210 and 2 wouldn't.
Kevin Barry
Well, I have also tried out the program and modified it. I wanted to find out the "longest" sequence of non-prime numbers up to one million.
Code:
#include <iostream>
using namespace std;
int main()
{
long int pos, factor;
long int last_num=1000000, first_num=1;
int count=0, max=0;
bool *primes = new bool[last_num+1];
for (pos=2; pos<(1+last_num/2); pos++)
{
if (primes[pos] == 1)
continue;
for (factor=1; pos*factor<=last_num; factor++)
{
if (factor != 1)
primes[pos*factor] = 1;
}
}
for (pos=2; pos<last_num+1; pos++)
{
if (primes[pos]) // keine Primzahl
count++;
else
{
if (max<count)
max=count;
count=0;
}
}
cout << max << endl;
delete [] primes;
}
You should check out Fermat's little theorem and see what creative problems you can come up with. If that's a bit much, maybe start with a little group theory then move on to finite abelian groups.
Kevin Barry
You need another algorithm, you won't beat the time with current one.
When checking if number "N" is prime or not, instead of trying to divide N by every number between 1 and sqrtf(N), try to divide by every prime number between 1 and sqrtf(N).
When iterating through possible divisors (when you test if number is prime) instead of starting at sqrtf(N) and going down to 2, start at 2 and go up to sqrtf(N) - number is singificantly more likely to be divisible by 2 than by its sqare root.
Instead of boolean arrays use stl containers to store prime numbers you found.
Use subroutines instead of of trying to jam everything into main().
Instead of allocating arrays with "new[]" use std::vector.
Don't forget that converting float (result of sqrtf) to int will result in rounding errors.
bool is not int, and C++ is not C. Comparing boolean value with 1 is incorrect.
When you allocate array of type (that does not have a computer) with "new[]", compiler does not initialize it, and it is filled with junk. Not initializing your variables and arrays is a bug.
You should check out Fermat's little theorem and see what creative problems you can come up with. If that's a bit much, maybe start with a little group theory then move on to finite abelian groups.
Kevin Barry
Well, I came to the program above (longest sequence of non-primary numbers) because I'm reading a book (Discrete Mathmatics) where "Bertrand's Postulate" was mentioned (for any integer n>1 there is at least one primary number p with n < p <= 2n). With the program above and the solution it becomes very clear but the proof is quite difficult (and not given in the book).
Markus
Last edited by markush; 07-16-2011 at 05:20 AM.
Reason: typo
I'm a bit off topic here
Well, I came to the program above (longest sequence of non-primary numbers) because I'm reading a book (Discrete Mathmatics) where "Bertrand's Postulate" was mentioned (for any integer n>1 there is at least one primary number p with n < p <= 2n). With the program above and the solution it becomes very clear but the proof is quite difficult (and not given in the book).
Markus
(Maybe on topic? It's about math...) The reason it's called a "postulate" is it seems right and hasn't been proven, but it also hasn't been disproven. A famous example is the parallel postulate, which has been proven to be unprovable and undisprovable.
Kevin Barry
More interesting for the problem in this thread seems to be the "Prime Number Theorem" (same book, same page ) which states that one can approximate the number of prime numbers lower than n with
Just came back from vacation, sorry to keep you waiting.
ta0kira: I'll check it, hopefully, I'll come out with something meaningful.
SigTerm: I'll take a look at your advice too, but it will take some time. Btw, more practice of C++: for me, it's not practice, I just started. (If I was skilled enough, I wouldn't have this problem).
markush: I'm very happy to hear any Math conversation, but little jealous that I'm not on same math level and can't read that book right now
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.