Project Euler #14 code output not coming - c++

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.

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)

SIGABRT:out of range when using stoi/stol

//I'm trying to solve this problem from HackerEarth: Binary Queries.
Though initially, the problem sounds easy to me but when actually submitted my code to run on all the test cases, my code is throwing SIGABRT error.
Upon checking the error, I found the error to be of type out of range. I'm unable to figure out how to resolve this:
problem: https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/range-query-2/
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
int main()
{
long N,Q,L,R,X;
int ch=0,buf;
unsigned long long intrim;
string str,cstr;
scanf("%ld %ld",&N,&Q);
cin.ignore();
getline(cin,str);
str.erase((remove_if(str.begin(),str.end(),(int(*)(int))isspace)),str.end()); // here its a key point
//cout<<str;
for(int i=0;i<Q;i++)
{
scanf("%d",&ch);
if(ch==1) // alter the X bit
{
scanf("%ld",&X);
if(str[X-1]==0){
str[X-1]=1;
}
else {
str[X-1]=0;
}
}
else if(ch==0)
{
scanf("%ld %ld",&L,&R);
cstr.append((str.begin()+L-1),str.begin()+R);
intrim=std::stoull(cstr,nullptr,2);
if(intrim%2==0){
cout<<"EVEN"<<endl;
}
else{
cout<<"ODD"<<endl;
}
cstr.clear();
}
}
}
stoi return an integer number
stol return a long int number
stoll return a long long int number
So, when you are converting a string to a number, thought about the possible range and use accordingly.

How can I fix a SPOJ time limit exceeded error on the STAMPS challenge?

Here is the link for the problem: http://www.spoj.com/problems/STAMPS/;
here is the ideone link for the current code: http://ideone.com/AcHfc6;
here is the code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,x,n,sum,sum2,count,i,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
cin>>x>>n;
sum=0;
int offer[n];
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
if(sum>=x)
{
sum2=0;
count=0;
for(i=n-1;i>=0;i--)
{
sum2+=offer[i];
if(sum2<=x)
count++;
else
break;
}
cout<<"Scenario #"<<j<<":"<<endl;
cout<<count<<endl;
cout<<endl;
}
else
{
cout<<"Scenario #"<<j<<":"<<endl;
cout<<"impossible"<<endl;
cout<<endl;
}
}
return 0;
}
The code gives the right answers for the given test cases but it causes TLE. I've tried converting my cin's and cout's to scanf/printf but, weirdly enough, the answers were not the same and I don't know how the answers were different from each other.
What's going wrong?
I suspect your main problem is here:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
You sort the data for every number that's entered. Also, you sort random data because you only have i rows of valid data in the array, not n rows. The sort should be done once, outside the loop:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
}
sort(offer,offer+n);

Finding the largest prime factor

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.