-3

I am stuck on trying to get the sieve to work. When I debug it, it tells me that stuff like 9 and 15 still evaluate to true when they go through the sieve. What causes this? Also, am I using the vector properly to get the highest prime factor?

#include <iostream>
#include <vector>
#include <math.h>

int main()
{
    long long n = 13195;
        long long sqrtn = sqrt(n);

        bool* boolarray = new bool[n];

        for(long long i = 0; i<=boolarray[sqrtn]; i++) {
                boolarray[i] = true;
        }

        long long x = 0;

        for(long long i=2; i<=sqrtn; i++) {
                if(boolarray[i]) {
                        for(long long j=pow(i, 2)+x*i; j<=n; j=pow(i, 2)+(++x*i))

                                        boolarray[j] = false;
                }
        }

        std::vector<long> primefactors;

        for(long long i = 0; i<=sqrtn; i++)
        {
                if(boolarray[i] && n % boolarray[i] == 0)
                        primefactors.push_back(i);
        }

        int answer = primefactors.back();

        printf("Answer: %i\n", answer);

        _sleep(10000);

        delete[] boolarray;

    return 0;
}
starblue
  • 55,348
  • 14
  • 97
  • 151
Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
  • 7
    Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (An then if necessary, you should construct a [minimal test-case](http://sscce.org).) – Oliver Charlesworth Mar 03 '13 at 21:06
  • I recommend you to revise your sieve algorithm, you can find many examples online. – ogzd Mar 03 '13 at 21:07
  • That's how I found out that the sieve wasn't working. – Tetramputechture Mar 03 '13 at 21:08
  • Also note that that is a terribly inefficient way to find the largest prime factor in general. – Daniel Fischer Mar 03 '13 at 22:23
  • @DanielFischer Would you be so kind as to tell me why? – Tetramputechture Mar 04 '13 at 00:29
  • 1
    Because sieving to `n` is `O(n*log log n)` work and requires `O(n)` space, but the largest prime factor is usually much smaller. If you completely factorise `n`, that's at most `O(sqrt(n))` work and requires only `O(log n)` space (if you forget smaller prime factors, `O((log n)²)` is an upper bound if you remember all prime factors). If the largest prime factor is relatively small, it's much less work. – Daniel Fischer Mar 04 '13 at 10:19
  • Consider that boolarray is an array of bool, so its values are all either 0 or 1. So how often will for(long long i = 0; i<=boolarray[sqrtn]; i++) iterate? At most twice. What about n % boolarray[i] == 0 when boolarray[i] can only be 0 or 1? Added a down vote because that code is just so bad. – gnasher729 Mar 26 '14 at 22:13

1 Answers1

1

The following is wrong:

                    for(long long j=pow(i, 2)+x*i; j<=n; j=pow(i, 2)+(++x*i))

Both the initial value and the update expression for j are incorrect. I leave it as an exercise to figure out what exactly is wrong, and how to fix it.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Ok I'll just spend a few more hours racking my brain and I'll get back to you. I already knew that was wrong. – Tetramputechture Mar 03 '13 at 21:11
  • How is this too localized? What the hell? I just want some help. – Tetramputechture Mar 03 '13 at 21:21
  • @Tetramputechture it's not locked so someone can still help you through comments if they feel like it (and many people are helpful like that) but SO is not for general debugging help, it's for focused questions – Stephen Lin Mar 03 '13 at 21:30