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.
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 do some recursive multiplication.
When the iteration number is more than 1 million, the completion time started to go up, why?
#include <time.h>
#include <stdio.h>
float num;
unsigned long i, j;
clock_t start, end;
int main(void)
{
start = clock();
for (j = 0; j<10000000; j++){
num = 1.000001E30f;
for (i = 0; i<100; i++){
num = num * 0.999915454854432f;
if (num == 0){
printf("zero\n");
}
}
//printf("%e\n", num);
//printf("%ld\n", j);
}
end = clock();
float cpu_time_used = ((float)(end - start))/CLOCKS_PER_SEC;
printf("%f", cpu_time_used);
return 0;
}
Compiled with GCC 7.3 on Windows 10
You keep multiplying an accumulator by 0.999915454854432f, thus bringing the value closer and closer to zero. You might be getting so close to zero that it becomes a denormal representation. That may trigger slower execution in the floating point hardware and can be a source of surprising performance bloat. Just a wild guess!
See the "Performance Issues" section in the above Wikipedia page.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.
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
#include <stdio.h>
int isPrime(int n){
int ndiv = 0;
int i;
for(i=1;i<=n;i=i+1){
if(n%i == 0){
ndiv = ndiv+1;
}
}
if(ndiv == 2){
return 1;
}
else{
return 0;
}
}
int nextPrime(int n){}
int main(){
int a = isPrime(7);
printf(a);
//printf(isPrime(4));
}
This code gives me a run time error, I think there's a problem here with the way I deal with data types while using a functions and the printf command, but I can't really figure it out. Help!
f in printf stands for "format". You need to supply a format string for printing: printf("%d\n", a)
Your isPrime is inefficient: you do not need to attempt dividing all the way up to the number itself. You could stop once you reach the square root of the number
Moreover, you could exit the loop early once you see that the number is not prime.
Once you fix these errors, your program would start running and producing the output that you expect.
Here is a small example of how to use printf. You can find more format specifiers here.
#include <stdio.h>
int main()
{
int a = 97;
int b = 98;
char hello[6] = "world";
printf("%d\n", a);
printf("%d\n", b);
printf("%s\n", hello);
return 0;
}
It is because your method of printing a variable is wrong. Here's the right one.
int main(){
int a = isPrime(7);
printf("%d",a);
}
I'm no C/C++ expert, but try
printf("%d", a);
%d is a format placeholder expecting an integer number, essentially.
That looks like an interesting isPrime function. Not very efficient at all, but different from what I have seen in the past. You can also loop over all the numbers between 1 and n, and just return false (or 0) if you find any that divise n. Or look up more efficient algorithms.
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;
}
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.