Thursday, June 2, 2016

A. Collatz conjecture The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 19, the sequence is 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

A. Collatz conjecture
The Collatz conjecture concerns what happens when we take any positive integer n and apply
the following algorithm:


The conjecture states that when this algorithm is continually applied, all positive integers
will eventually reach 1. For example, if n = 19, the sequence is
19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1



#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/wait.h>

int main()
{
char c,num[10];
int n,len,i;
while(1)
{
printf("\nWhat is your number");
gets(num);//Read input from console
if(strcmp(num,"q")==0)// Quit if input is q
exit(0);
n=atoi(num);// String to number conversion
if(n>2)// Wrong input checking
{
        collatz(n);// Function calling
}
else
{
        printf("enter valid input...\n");
}

}
return 0;

}

int collatz(int i)// Called Function
{

pid_t child_pid, wpid;

int status = 0;

        if ((child_pid = fork()) == 0)// Child process creation

{

while(i != 1){

printf("%d\t", i);

if(i%2 == 0){// If n is even

i /= 2;

}

else{// if n is odd

i = 3*i + 1;

}

}

printf("1\n");
exit(1);

}
while ((wpid = wait(&status)) > 0)

{

printf("Child process exited\n");

}

return;

}

1 comment:

  1. Slot Machines - Mr. McD
    Slot Machines is 경기도 출장샵 a video game developed 춘천 출장샵 by Software 제주 출장안마 Creations for the Xbox One. and they did this with the PS4 and Xbox One version, and 제주도 출장마사지 the Genesis 경주 출장안마 version

    ReplyDelete