Project euler number 10 in C [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 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.

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 #10. getting wrong answer [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 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.

Project Euler #14: What's wrong with this code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
This is related to the "Project Euler Problem 14"
As I think, my logic and the code is good enough,but it may take few seconds to give the answer.
But when i run this code, the program stops when "starting_number" is around 103152(i can't remember the number exactly).
Can anybody please have a look at this code and, tell me where and what's wrong with this code.
Here's the code :
#include<stdio.h>
int starting_number;
int number_of_terms;
int j=0,k=0;
int term;
int main(){
for(starting_number=2;starting_number<1000000;starting_number++){
term = starting_number;
number_of_terms = 1;
while(1){
{
if(term%2==0){
term = term/2;
number_of_terms++;
} else if(term%2!=0){
term = 3*term + 1;
number_of_terms++;
}
}
if(term == 1) break;
}
if(j>=number_of_terms) //finding which chain is longer
j=j;
else if(j< number_of_terms) {
j= number_of_terms;
k=starting_number;
}
printf("\n%d",starting_number);
}
printf("\n%d(%d)\n",k,j);
return 0;
}
This one's mildly tricky, but your problem is here:
if(term == 1) break;
If the variable term becomes very large (as it can easily do) then it can overflow the int datatype.
When this happens term becomes negative. The C language modulus of a negative odd number is itself negative. Therefore, the end condition for your loop is never met.
Solve this problem by using a larger data type such as unsigned long long.
A less convoluted version of your code would appear as follows. Note that I have eliminated the global variables (those outside of your main function) because global variables are evil. I've replaced your infinite while-loop with a loop that uses an end condition. I've reduced duplication of code within the while-loop. I've eliminated the j=j case. Since printf is a slow function to run, I've commented out the prinft you had in the for loop, which improves the run-time significantly.
#include <stdio.h>
int main(){
int number_of_terms;
unsigned long long term;
int j=0,k=0;
for(int starting_number=2;starting_number<1000000;++starting_number){
term = starting_number;
number_of_terms = 1;
while(term!=1){
if(term%2==0)
term /= 2;
else
term = 3*term + 1;
number_of_terms++;
}
if(j<number_of_terms){ //finding which chain is longer
j = number_of_terms;
k = starting_number;
}
//printf("\n%d",starting_number);
}
printf("\n%d(%d)\n",k,j);
return 0;
}
And, indeed, using unsigned long long solves the problem.

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;
}

Resources