-1

This is my code :

#include <iostream>

using namespace std;

int main()
{
    long int x = 1;
    long int res;

    while (x<600851475143)
    {
        x++;
        if(600851475143%x==0)
        {
            res=x;
            cout<<x<<"\n";
        }
    }
}

I don't know whats wrong with it but it gives me this output :

839
1471
6857
59569
104441
486847
1234169
5753023
10086647
87625999
408464633
716151937
-716151937
-408464633
-87625999
-10086647
-5753023
-1234169
-486847
-104441
-59569
-6857
-1471
-839
-71
-1
Floating point exception

Process returned 136 (0x88)  execution time : 156.566 s
Press ENTER to continue.

and when i substitute 600851475143 with 13195 [ which was in the example ]... it works fine...and gives me this output :

5
11
55
11149

Process returned 0 (0x0) execution time : 0.005 s
Press ENTER to continue.

I don't know what i am doing wrong... :/ Perhaps my previous program didn't run properly...i tried it with int in the beginning and then changed it to long int...No difference...

Rohan Bojja
  • 655
  • 1
  • 16
  • 35

5 Answers5

2

Negative values are due to overflow. Start by using unsigned integers instead of signed integers. In addition to that, in 32 bit computers, long int and int types are both 32 bits.

unsigned long long int x = 1;
unsigned long long int res;

Also, you could emphasize the fact that those constants are unsigned

while (x<600851475143U)
    {
        x++;
        if(600851475143U%x==0)
        {
            res=x;
            cout<<x<<"\n";
        }
    }
Tom
  • 43,810
  • 29
  • 138
  • 169
2

When your positive number suddenly turns negative after ++, it's a sure sign of integer overflow. You need to pick a data type that is capable of holding larger integers, e.g. long long.

On a side note, your algorithm is not searching for the largest prime factor, as required in the problem. It searches for the largest factor, which is not necessarily a prime one.

By the way, if you can prove that stopping the search upon reaching the square root of the number being factored is correct, you could speed up your program a great deal.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The code below will show all the prime factors of a number entered by user.But 600851475143 is a too much big number to find its prime factors. Modify this program to see 600851475143's prime factors and also modify it to find out the greatest prime factor of them. I have learned c++ for two months only, so i can help you this much only.All the best.

            #include<iostream.h>
            #include<conio.h>
            class primefactor
                 {
                    unsigned long int a;
                  public:
                            void factor();

                  };
           void primefactor::factor() 
                 {
                     cout<<"Enter any number to see its prime factors";
                     cin>>a;
                     int count=0;
                     int count1=0;
                     for(unsigned long int loop1=a;loop1>=1;loop1--)
                         {
                            if(a%loop1==0)
                              {
                                 for(unsigned long int loop=loop1;loop>=1;loop--)
                                       {
                                         if(loop1%loop==0)
                                              {
                                                  count++;
                                               }
                                         }
                             if(count==2)
                                {
                                    count1++;
                                    cout<<loop1<<"\t";
                                 } 
                          }
                     count=0;
                    }
                       cout<<"\n"<<"There are"<<count1<<"\t"<<"prime factors";
                 }

           void main()
                 {
                    primefactor k;
                       clrscr();
                    k.factor();
                       getch();
                  }
Deepeshkumar
  • 395
  • 3
  • 13
1

Your variable types can't hold 12-figure numbers. Typical unsigned long int can store from 0 to 4,294,967,295, for signed - −2,147,483,648 to 2,147,483,647.

Alexander
  • 8,117
  • 1
  • 35
  • 46
1

try using an unsigned long long, it should store at least 18,446,744,073,709,551,615 (which number I can't even imagine).

vulkanino
  • 9,074
  • 7
  • 44
  • 71