Finding the largest prime factor - c++

I was working on the following problem from Project Euler.
What is the largest prime factor of the number 600851475143?
I have come up with the following solution.
#include<iostream>
#include<cstdlib>
#include <limits>
#include <math.h>
/**Project Euler:What is the largest prime factor of the number 600851475143**/
using namespace std;
bool isPrime(int n){
int sq,a=2;
sq=sqrt(n);
while(a<sq)
{
if(n%a==0)
return false;
}
return true;
}
int main() {
int c=2,max_prime;
long long d=600851475143;
int e=sqrt(d);
while(c<e)
{
if(isPrime(c))
{
if(d%c==0)
max_prime=c;
}
c++;
}
cout<<max_prime;
return 0;
}
There is no compilation error in the program but it is taking a lot of time in running. I have looked at other solutions but am not able to find out the mistake in my solution.I suspect there is something wrong because of the large number involved. What could that be? Any help appreciated.

Your isPrime function never increments a, so you're in an endless loop there.

In your isPrime method you're not changing any of the variables that control the while condition within the body of the while statement, so the loop wil run forever.

Related

infinity while loop when have recursive function

I'm solving a problem. I want a function that returns all ways to make different positive int number plus equal to that number, for example 6 will be 1+5 ,2+3+1,2+4 so will be 3
but my solution return infinity loop
#include <iostream>
using namespace std;
int find(int num,int before)
{
int first=1;
int count=1;
int end =num-1;
if(end-first==0) return count;
while(end-first!=1&&end-first!=0)
{
if(end==before||first==before) continue;
first++;
end--;
}
before=first;
return count+find(end,before);
}
int main()
{
int a;
cin>>a;
int x=find(a,1);
cout<<x;
}
i try cout "a" in loop and repeat forever. Please help me.
EDIT: My code just solve a piece of problem, so it not solution, i'll try to close topic, thanks all
In this concept first will always be 1 and before will always be 1 because the first is never incremented here as the instruction is not reached.
first is initialized by 1 and before by 1, that means before==first is true and the loop will ignore all other instructions after continue.
and since you are comparing end==before||first==before this will always be true, because even when end==before is false the second test will be true. The test logic of false||true is true.

Why does the program to find the largest number formed using elements of an array not work?

Visit https://www.interviewbit.com/problems/largest-number/ for the question...
Now I wrote the below code to solve the question (although I used an array to store the number, will do the storing in strings part later..)-
So in this algorithm, I basically used quicksort but with a twist, I changed the definition of greater than or lesser than of two numbers say X, Y such that if the number formed by using X first and Y second or XY is >= YX then greater than(X, Y) is true
In the present scenario, the code is giving runtime error, which I can't understand why, also after a bit of debugging as shown in the comments, still the answer is not coming as expected.
#include <iostream>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h>
using namespace std ;
bool greaterthan(int a,int b)
{
int n1,n2,s1,s2;
n1=((int )log10(a))+1;
n2=((int)log10(b))+1;
s1=a*((int )pow(10,n2))+b;
s2=a + ((int )pow(10,n1))*b;
if(s1>=s2){return true;}
else{return false;}
}
int spartitions(vector<int >&B,int s , int e)
{
int pivot = B[e];
int pin =s;
int i;
for(i=s;i<=e;i++) //if i change this to i<e
{
if(B[pin]>=pivot)
{swap(B[pin],B[i]);
pin++;
}
// and add swap(B[pin],B[e]);
}
return pin-1; // and return pin here then it works but not give correct output
}
int prand(vector<int >&B,int s ,int e)
{
srand(time(NULL));
int n = rand()%(e-s+1)+s;
swap(B[n],B[e]);
int pin = spartitions(B,s,e);
return pin;
}
void qsort(vector<int >&B,int s, int e )
{
if(s<e){
int p= prand(B,s,e);
qsort(B,s,p-1);
qsort(B,p+1,e);
}
}
vector<int> largestnumber(vector<int >&A)
{
int n =A.size();
vector<int >B(n);
B=A;
qsort(B,0,n-1);
return B;
}
int main()
{
int n;
cin>>n;
vector<int>A(n);
int i;
for(i=0;i<n;i++)
{
cin>>A[i];
}
vector<int >B(n);
B=largestnumber(A);
for(i=0;i<n;i++)
{
cout<<B[i];
}
}
Please Help as I am a newbie in programming and can't figure this out from like 3-4 hours ...??
Would really appreciate if someone can correct my code only and not give a different algorithm, as I want this algorithm to be corrected.
Your self-written qsort function recursively calls itself, which adds more things to the stack, which only has so much space. When the list is too big, there will be too many function calls in the stack and it overflows. That's why anything less than 5 for the first input (which is for n) works fine but as soon as you exceed that, you get a runtime error. Consider not using a recursive function call.
Edit: Enabling optimisation also seems to fix this issue.
This may not work depending on the compiler and how it optimises. (Works on MSVC)

For and while loop not working in a globally defined function

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int help(int i,int money,int denomination[]){
int sum=0;
int j=0;
while(i>0){
if(i&1) sum+=denomination[j];
i=i>>1;
j+=1;
}
return sum==money?1:0;
}
int ans(int numOfNotes,int money,int denomination[]){
for(int i=0;i<(1<<numOfNotes);i++){
if(help(i,money,denomination)){
return 1;
}
}
return 0;
}
int main() {
int testCases,numOfNotes,money;
cin>>testCases;
while(testCases>0){
cin>>numOfNotes>>money;
int denomination[numOfNotes];
int i=0;
while(numOfNotes){
cin>>denomination[i];
i++;
numOfNotes--;
}
testCases--;
ans(numOfNotes,money,denomination)==1?cout<<"Yes"<<endl:cout<<"No"<<endl;
}
return 0;
}
If there exists a subset of array denomination such that it amounts to money, program should show "Yes", else "No".
But for the following simple input
1
3 3
1
1
1
output is coming out to be
No
whereas it should be
Yes
According to me, the for and while loops are not working in the ans and help functions. Is there any other bug in the program?
You're modifying numOfNotes in the inner while loop in main, which means its value is 0 when later passed to ans. The loops in the functions are working, you're just giving them other limits than you expected.
You can easily solve issues like this yourself by stepping through your program in a debugger and inspecting variable values and control flow along the way. This is a vital programming skill. See also How to debug small programs by Eric Lippert.
Unrelated to the problem at hand, but please also see these SO questions about what's wrong with including bits/stdc++.h and declaring using namespace std;.

Project Euler #14 code output not coming

I used the following code for solving problem#14 but for some strange reason it gives no output.Maybe its taking too long to run???
P.S.I know that max is not supposed to be the answer but still there is no output anyways whereas for smaller values like i<100 I get the output.
#include <iostream>
long collatz(long);
int main()
{
using namespace std;
long i=3,max;
for(i=3;i<1000000;i++)
{
max=collatz(i-1);
if(collatz(i)>collatz(i-1))
{
max=collatz(i);
}
else
{
max=collatz(i-1);
}
}
cout<<max<<endl;
cin.clear();
cin.get();
}
long collatz(long n)
{
int count=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
count+=1;
}
else
{
n=3*n+1;
}
}
return count;
}
If you call collatz with n = 113383, you get overflow and n becomes negative from which it never recovers. So you have an infinite loop as it will never be 1. You need to use a long long inside collatz.
However, your collatz functions has other problems as pointed out by others. Also, your logic for the loop in main is not correct. You are resetting max each time through the loop. So, the result you report would be either collatz(999999) or collatz(999998). But that is not be the correct answer.

why std::map throws an exception?

I run this code using both gpp and microsoft compiler but in both case I'v got an exception
but I can't understand why!
this is my code:
#include <iostream>
#include <map>
using namespace std;
map<int,int> fib;
int fibo(int i)
{
if (!fib.count(i))
{
fib.insert(pair<int, int>(i,fibo(i-1)+fibo(i-2)));
}
return fib[i];
}
int r(int i)
{
if(i<3)
{
return i;
}
else
{
return fibo(i)+r(i-2);
}
}
int main()
{
fib.insert(pair<int, int>(0,1));
fib.insert(pair<int, int>(1,1));
int a,b,n;
cin>>a>>b;
n=b-a;
int fiba=fibo(a);
int fibaa=fibo(a-1);
cout << (r(n+1)*fiba)+(r(n)*fibaa);
return 0;
}
can anyone help me?
I debugged this code and I found that fib.insert(pair<int, int>(i,fibo(i-1)+fibo(i-2))); doesn't work.
I've got Stack Overflow exception when I ran your code, obviously because of too deep recursion.
You should either increase stack size (but you can later choose to input a larger number and get the same exception again), or convert this algorithm into a non-recursive one (for example see this one: http://www.codeproject.com/Tips/109443/Fibonacci-Recursive-and-Non-Recursive-C)
Did you try to input some negative numbers? You don't do any check on the inputs you pass to your program, and in
return fib[i];
you will receive an error if you try to access non-existent locations.