project euler #10. getting wrong answer [closed] - c

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.

Related

wrong output C after successful attempts [closed]

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 2 years ago.
Improve this question
So i'm trying to solve this codeforce problem https://codeforces.com/contest/431/problem/A
.Basically i input 4 integers(a[0]...a[3]) and an array of integers between 1 and 4 then i need to output the sum of the string values according to the 4 initial integers.(check the codeforce's exemples)
So my code did work on the 5 initial tests but i had a wrong output on the 6th test
enter image description here
Here's the code
#include <stdio.h>
int main()
{
int test=1;
long s,result=0;
long a[3];
int i,x;
for (i=0;i<4;i++)
{
scanf("%d",&a[i]);
if (a[i]==0)
test=0;
}
scanf("%d",&s);
while (s!=0)
{
if (test==0)
break;
x=s%10;
s=s/10;
result=a[x-1]+result;
}
printf("%d",result);
return 0;
}
Your help would be much appreciated.
There have several problems with your code. But the most severe problem for which you are getting wrong output because you are using "%d" format specifier for long values, but its "%ld" actually. Using %ld will solve the problem.
Leaving rest of the problems for you to find out. Happy coding!

C Prime Number crashes at 64901 [closed]

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
#include <stdio.h>
#include <stdlib.h>
#define size 20000000
int prim[size];
int i, zahl, zaehler, erg;
int sieve(int zahl, int prim[], int zaehler) {
if(zahl == 2000000)
return 1;
for(i=0; i<=zaehler; i++) {
erg = zahl%prim[i];
if(erg==0) {
zahl++;
return sieve(zahl, prim, zaehler);
}
}
zaehler++;
prim[zaehler]=zahl;
zahl++;
printf("%d\n", prim[zaehler]);
return sieve(zahl, prim, zaehler);
}
int main(){
zaehler = 0;
zahl = 2;
for(i=0;i<size;i++)
prim[i]=2;
sieve(zahl, prim, zaehler);
}
When trying to calculate prime numbers, when i run this code, it always crushes at the number 64901.
What might be the problem?
Ironically, this is literally a stack overflow due to recursion. You can make your stack large (which will only delay the issue), or change from a recursive solution to an iterative one.
(and for what it's worth, some debuggers won't be able to help you in this situation. And it's very difficult to beginners in C to understand what is going wrong until the first time they hit this problem. So congrats! You're leveling up in C)
A cheap way to verify it's indeed a stack overflow is to create extra memory on your stack in the recursive function and see if the number it crashes on changes from 64901. My guess is if you put like char dummy[2048] in there, it will crash much sooner.

Project euler number 10 in C [closed]

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.

How to generate different random numbers in a for loop with C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to choose the distinct initial seeds to generate different random numbers in [0,1] in a for loop with C?
Vague answer to a vague question :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int i;
srand(time(NULL));
for(i = 0; i < 100; i++){
printf("%i ", rand()%2);
}
return 0;
}

C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
C prog that generates/output 3 numbers (any combination from 1 - 20) when added is equal to 20. Hope you could help me with this one. Thanks in advance.
int main(){
int num1,num2,num3,sum;
do{
printf("%i+%i+%i=%i\n",num1,num2,num3,sum);
} while(sum=20); getch();
}
Try this one. You should be able to modify it according to your needs.
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
for(k=1;k<=20;k++)
if(i+j+k==20)
printf("%d %d %d\n",i,j,k);
return 0;
}

Resources