Strange behavior for loop over a million [closed] - c

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.

Related

Recursion stops after some executions [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 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.

Thinking of a code to show if a number is prime or non prime? [closed]

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.

Taking power with loop(with-out pow() function) [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 4 years ago.
Improve this question
Im trying the powers of an integer. For example if my integers is 2 first 10 power is 2^1,2^2...2^10. Im using
while (expnt < 10)
{
preExpnt = expnt;
while (preExpnt)
{
preExpnt *= num;
printf("%lld\n", preExpnt);
}
expnt++;
}
but it doesn't work.`
Here is a way you could achieve your purpose.
int num = 2; // for example
int out = 1;
for (int exp = 1; exp <= 10; exp++)
{
out *= num;
printf("%d\n", out);
}
Remarks about your code:
Your inner while loop is infinite if num and expnt are both different from 0.
Assigning preExpnt to the value of expnt at each step and multiplying by num would display a something like: 1*n 2*n 3*n 4*n ... if expnt starts at 1.

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 #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.

Resources