trailing zeros in a factorial.when i run it on ideone.com i'm getting runtime error with infinite output. Even SPOJ is also not accepting - c++

#include <iostream>
#include <cmath>
using namespace std;
int func(int n){
int count=0;
float t=log(n)*0.621;
int k=(int)t;
int temp;
for(int i=1;i<=k;i++){
int power=1;
for(int j=0;j<i;j++){
power=power*5;
}
temp=(int)(n/power);
count=count+temp;
}
return count;
}
int main(){
int t;
cin>>t;
for (int i=0;i<t;i++){
int n;
cin>>n;
cout<<func(n)<<'\n';
}
return 0;
}
I'm getting correct answer for few test cases.The error i'm getting in ideone is
Runtime error time: 0 memory: 3100 signal:25
33628713
33628713
33628713
33628713
33628713
33628713
with infinite output when no input is given

I don't know what the logarithm is doing in your code. You somehow use it to determine when to stop dividing by powers of 5. It appears to be an approximation, and if you only get correct answers sometimes, then I expect that the approximation's inaccuracy is working against you.
You don't need the logarithm. You could simply stop the loop when you determine that there are no more factors of powers of 5 remaining. Think about how you could detect that using the values you've already calculated.
You don't need to approximate anything. A page at Purplemath explains and demonstrates how to calculate the number of trailing zeroes in a factorial number. Read the article and see whether you can recognize how the technique described there corresponds to parts of your code, and also notice how there are no logarithms involved on that page.

Related

why my recursion code to print no in increasing order not working for input greater than 10^5?

here is my code -
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void printno(ll n){
if(n==1){
cout<<1<<endl;
return;
}
printno(n-1);
cout<<n<<"\t";
}
int main(){
ll n;
cin>>n;
printno(n);
return 0;
}
i am using vs code. and my code not working for input greater than 10^5. what is the problem in my code? and how can i get output for input greater than 10^7.
You probably ran out of stack space on your platform by trying to have more than 100,000 recursive calls on the stack at once. The website we are on is named after the thing that happened to your program. I'm not sure what your end goal is here, but try to come up with a way to do it without recursion or without recursing so many times (e.g. use a simple for loop).

Why by using n&(n-1) is not able to calculate number of 1's bits?

I found a video on youtube that says by using n&(n-1) we can able to count number of 1's bits and it's much faster than - right shifting the bits by one and then checking the last bit . When I wrote the code using this method to calculate number of bits it's giving output as 9 instead of 3 - if we 01011 as parameter.
I debug the program and not able to find why it's happening so ?I am confused when in while loop condition , both A and A-1 is 512, it get out of loop even A&(A-1) = 1000000000 not 0 (zero).
If anyone knows what I am missing please let me know .
#include <iostream>
using namespace std;
int bits(unsigned int A){
int counter=0;
unsigned int t;
while (A&(A-1))
{
counter++;
A--;
}
return counter;
}
int main()
{
unsigned int A=01011;
int res=bits(A);
cout<<"Result is "<<res<<"\n";
return 0;
}
Thanking You
Yours Truly,
Rishabh Raghwendra
This is the Kerningham trick to calculate the number of bits without shifting. (Original author precedes Kerningham by a decade, but mr K popularised it).
n&(n-1) peels off the least significant one, meaning that one should write the result back to n.
while (n) {
++counter;
n&=n-1;
}

Written code in linear time as solution to dp problem but getting illogical answers

I have written the following code to solve this hackerrank problem:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,k,x;
cin>>n>>k>>x;
double a,b;
long int answer[n-1];
if (x == 1)
answer[1] = k-1;
else
answer[1] = k-2;
for (int i=3;i<=n-1;i++)
{
a = k-1;
b = i-1;
answer[i-1] = pow(a,b)-answer[i-2];
}
cout<<answer[n-2]<<endl;
return 0;
}
I have tried debugging it for hours and I find no inconsistency with my O(N) time solution. I am getting the same negative answer for large inputs of n,k,x: like 50 50 50
I am not asking this for points but for educational purposes.
It is because of an overflow. Basically you are trying to store numbers that are too big for int to handle. You can use long long to store bigger numbers but even those have limit. I see the problem gives a number to use it as a mod. You can use that number to make sure your answer doesn't overflow by moding your answers over that number.

input a,b output all armstrong numbers in [a,b] in 1s and 1<=a<=b<=10^7

This is my code about c++
I do on IDE of my school and when i run the code on it.It have 10 test to run the program and it said just 8 test correct and 2 test is time limit exceeded
because it allow the program run in 1 second so i want to ask are they another way to make my program run faster.I'm the beginer with stack overflow so i don't use to do this if there anything wrong pleas tell me thank you
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main()
long long a,b;
cin>>a>>b;
for(long long i=a;i<=b;i++){
int digit=0;
long long number=i,sum=0;
while(number!=0){
number=number/10;
digit++;
}
number=i;
while(number!=0){
sum=sum+(pow((number%10),digit));
number=number/10;
}
number=i;
if(number==sum){
cout<<number<<" ";
}
}
}
Probably using logarithm in base 10, if you use logarithm in base 10 trunc plus 1 of a number you obtain the number of digits. And use a vector initialized with the pows of 0,1,2,...9 to digit potency instead reacalculate in every interation.

How does the cout statement affect the O/P of the code written?

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main() {
int t;
double n;
cin>>t;
while(t--)
{
cin>>n;
double x;
for(int i=1;i<=10000;i++)
{
x=n*i;
if(x==ceilf(x))
{
cout<<i<<endl;
break;
}
}
}
return 0;
}
For I/P:
3
5
2.98
3.16
O/P:
1
If my code is:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main() {
int t;
double n;
cin>>t;
while(t--)
{
cin>>n;
double x;
for(int i=1;i<=10000;i++)
{
x=n*i;
cout<<"";//only this statement is added;
if(x==ceilf(x))
{
cout<<i<<endl;
break;
}
}
}
return 0;
}
For the same input O/P is:
1
50
25
The only extra line added in 2nd code is: cout<<"";
Can anyone please help in finding why there is such a difference in output just because of the cout statement added in the 2nd code?
Well this is a veritable Heisenbug. I've tried to strip your code down to a minimal replicating example, and ended up with this (http://ideone.com/mFgs0S):
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float n;
cin >> n; // this input is needed to reproduce, but the value doesn't matter
n = 2.98; // overwrite the input value
cout << ""; // comment this out => y = z = 149
float x = n * 50; // 149
float y = ceilf(x); // 150
cout << ""; // comment this out => y = z = 150
float z = ceilf(x); // 149
cout << "x:" << x << " y:" << y << " z:" << z << endl;
}
The behaviour of ceilf appears to depend on the particular sequence of iostream operations that occur around it. Unfortunately I don't have the means to debug in any more detail at the moment, but maybe this will help someone else to figure out what's going on. Regardless, it seems almost certain that it's a bug in gcc-4.9.2 and gcc-5.1. (You can check on ideone that you don't get this behaviour in gcc-4.3.2.)
You're probably getting an issue with floating point representations - which is to say that computers cannot perfectly represent all fractions. So while you see 50, the result is probably something closer to 50.00000000001. This is a pretty common problem you'll run across when dealing with doubles and floats.
A common way to deal with it is to define a very small constant (in mathematical terms this is Epsilon, a number which is simply "small enough")
const double EPSILON = 0.000000001;
And then your comparison will change from
if (x==ceilf(x))
to something like
double difference = fabs(x - ceilf(x));
if (difference < EPSILON)
This will smooth out those tiny inaccuracies in your doubles.
"Comparing for equality
Floating point math is not exact. Simple values like 0.2 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. Different compilers and CPU architectures store temporary results at different precisions, so results will differ depending on the details of your environment. If you do a calculation and then compare the results against some expected value it is highly unlikely that you will get exactly the result you intended.
In other words, if you do a calculation and then do this comparison:
if (result == expectedResult)
then it is unlikely that the comparison will be true. If the comparison is true then it is probably unstable – tiny changes in the input values, compiler, or CPU may change the result and make the comparison be false."
From http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm
Hope this answers your question.
Also you had a problem with
if(x==ceilf(x))
ceilf() returns a float value and x you have declared as a double.
Refer to problems in floating point comparison as to why that wont work.
change x to float and the program runs fine,
I made a plain try on my laptop and even online compilers.
g++ (4.9.2-10) gave the desired output (3 outputs), along with online compiler at geeksforgeeks.org. However, ideone, codechef did not gave the right output.
All I can infer is that online compilers name their compiler as "C++(gcc)" and give wrong output. While, geeksforgeeks.org, which names the compiler as "C++" runs perfectly, along with g++ (as tested on Linux).
So, we could arrive at a hypothesis that they use gcc to compile C++ code as a method suggested at this link. :)