Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
why is my code http://ideone.com/zm6hP7 not satisfying all test cases in the problem https://www.hackerrank.com/contests/opc-a/challenges/infinite-series ?
I have downloaded unsatisfied test cases and still can't find any problem in my code....can somebody tell me what the problem is???
#include<stdio.h>`
#define m 1000000007
int main()
{
long long int t;
scanf("%lld",&t);
for(long long int i=0;i<t;i++)
{
long long int l,r;
scanf("%lld %lld",&l,&r);
long long int x,y;
if(l%2==0)
x=(((l/2)%m)*((l-1)%m))%m;
else
x=((l%m)*(((l-1)/2)%m))%m;
if(r%2==0)
y=(((r/2)%m)*((r+1)%m))%m;
else
y=((r%m)*(((r+1)/2)%m))%m;
printf("%lld\n",y-x);
}
return 0;
}
The likely mistake is that y-x is negative when y(mod m) < x(mod m). This can be corrected by adding this at the end, before the printf() statement:
if(y-x < 0)
{
y += m;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to find the largest prime factor of a number. The code stops working (at least I think like that) after some executions. printf parts are for debugging.
The number is 600851475143.
long recursed(long i, long j){
if (j == 1){
return i;
}
else if (i%j != 0){
printf("Else if i : %ld %ld\n", i, j);
return recursed(i, j-1);
}
else{
i /= j;
printf("Else i : %ld\n", i);
return recursed(i, i-1);
}
}
A typical 32-bit long tops out at 4,294,967,295.
You gave it 600,851,475,143, which is much much larger.
Be sure you compile on x64, and use unsigned long long.
Then check for overflows.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to learn how to code by myself and is experiencing some difficulty in calculations. Can someone please explain why the pf always return 0 in the below?
int main(void)
{
//solicit input from users
long long int num = get_long_long("Credit card no: ");
eprintf("%lld\n",num);
//initialize
int i =0;
int j =0;
int counter =0;
string status;
//find length of input
while (num>0)
{
num/= 10;
counter++;
}
printf("counter is %i\n",counter);
//Identify card type by prefix
int power=(counter-2);
eprintf("power is %i\n", power);
int dp = pow(10,power);
eprintf("divofp is %i\n", dp);
//prefix=num
long long int pf=(num/dp);
eprintf("pf is %lld\n",pf);
}
pf will always be zero, because num is set to zero at the end of your while loop.
Therefore num/anything will always equal zero.
A good method of debugging, is to step through the code line by line, and look at the values of your variables at each point in time.
This can help you narrow down problems like this.
The problem is where you get the length of your number:
while (num>0)
num/=10;
num will always be 0 after this and thus your final expression will result in 0 because 0/x = 0 (x != 0).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to program a sequence x(n) in C with the following conditions:
x(0)=x
x(n)=1 if x(n-1)=1
x(n)=3*x(n-1)+1 if x(n-1)!=1 and x(n-1) not even
x(n)=x(n-1)/2 if x(n-1) even
I tried the following:
int sequence(int x, int n){
if(n==0){
return x;
}
if (sequence(x,n-1)==1){
return 1;
}
if((sequence(x,n-1)!=1)&&((sequence(x,n-1)%2)!=0)){
return 3*sequence(x,n-1)+1;
}
if((sequence(x,n-1)%2)==0){
return sequence(x,n-1)/2;
}
}
It should give me the n-th element of the sequence with the starting point x. However, it does not work...
Your code does what you proposed it to do. Maybe your original logic is flawed.
f(x,0)=x
f(x,n)=1 if f(x,n-1)=1
f(x,n)=3*f(x,n-1)+1 if f(x,n-1)!=1 and f(x,n-1) not even
f(x,n)=f(x,n-1)/2 otherwise
You can clean up your code somewhat to improve its readability and make it slightly faster/better.
int sequence(int x, int n){
if(n==0){
return x;
}
if (sequence(x,n-1)==1){
return 1;
}
if((sequence(x,n-1)%2)!=0){
return 3*sequence(x,n-1)+1;
}
return sequence(x,n-1)/2;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i cant get the following code to run, ie it dosent give out any output
the objective is to find the sum of all primes below 2 million,
#include <stdio.h>
#include <math.h>
int is_prime(long long int i)
{
long long int n;
if(i==2)
return 1;
for(n=2;n<=sqrt(i);n++)
if(i%n==0)
return 0;
return 1;
}
int main()
{
long long int s=0,i=2;
for(i<2000000;i++;)
{
if(is_prime(i))
s=s+i;
}
printf("sum: %lli",s);
return 0;
}
You're using the for loop wrong. A for loop looks like this:
for(initialization; test expression; update)
But you wrote
for(i<2000000;i++;)
which should be
for(;i<2000000;i++)
ie, skip initialization, on each iteration test for i<2000000 and increment.
Your problem is that you are essentially factorising every integer up to two million for this. It's not that your code isn't working, it just is running very very slowly. If you attach a debugger to it you will most likely be at line
if(i%n==0)
most of the time.
Implement the Sieve of Eratostenes. Will be the most effective here.
Also your loop for(i<2000000;i++;) will run for 2^64 cycles.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i wrote the following code for the euler's project problem 10. Although am getting the correct numbers of prime numbers(as checked by the code given in a similar question) but the sum output is coming incorrect
the code is
#include<stdio.h>
#include<stdbool.h>
#define upper 2000000
int main(void)
{
bool prime[upper];
long long sum=0, i,k;
for(i=0;i<upper;i++)
{
prime[i]=true;
}
for(i=2;i<upper;i++)
{
if(prime[i])
{
for(k=2;i*k<upper;k++)
{
prime[k*i]=false;
}
}
}
long count=0;
for(i=2;i<upper;i++)
{
if(prime[i])
{
count++;
sum +=i;
}
}
printf("%d %d",count,sum);
return 0;
}
printf("%d %d",count,sum);
is your problem. The sum gets calculated correctly but you use the wrong format string. Check the printf format, especially what you need to output a long long.