CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2010
    Posts
    2

    loop issue with prime number program

    Hi I'm writing a program that find the highest prime factor for a given number.

    Here's the code:

    #include<iostream>
    using namespace std;

    int main () {
    _int64 p, x, d, s, f;
    p = 600851475143;
    x = 0;
    d = 0;
    s = 0;
    f = 0;
    for (x=2; x < p; x++) {
    if (p % x == 0) {
    s = p/x;
    f = 0;
    d = 2;
    while ((d < s) && (f = 0)) {
    if (s % d == 0) {
    f=1;
    d++;
    }
    }
    if (f != 1) {
    cout << s;
    cin >> x;
    }
    }
    }
    return 0;
    }


    The problem is that when it reaches the second loop it just skips it out even though d = 2, s = 8462696833 and f = 0. Is the while statement incorrect? I want it to loop until d isn't less than s or f doesn't equal 0.

    Any help would be greatly appreciated, thanks.

  2. #2
    Join Date
    Jul 2010
    Posts
    2

    Re: loop issue with prime number program

    I realized my mistake, should have been f==0 not f=0. Also the d++ should be outside the if statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured