1 Sugar functions

  • Find the root of the equation \(2x^3 - 4x^2 + 3x - 6 = 0\). Search by dichotomy in the interval [-10, 10]
#include <stdio.h>
#include <math.h>
int main()
{
    // find the root of the equation 2x^3 - 4x^2 + 3x - 6 = 0
    // search by dichotomy in [-10, 10]
    float x0,x1,x2,fx0,fx1,fx2;
    do
    {
        printf("enter x1 & x2:");
        scanf("%f,%f",&x1,&x2);
        fx1=x1*((2*x1-4)*x1+3)-6;
        fx2=x2*((2*x2-4)*x2+3)-6;
    }while(fx1*fx2>0);

    do
    {
        x0=(x1+x2)/2;
        fx0=x0*((2*x0-4)*x0+3)-6;
        if ((fx0*fx1)<0){
            x2=x0;
            fx2=fx0;
        }
        else{
            x1=x0;
            fx1=fx0;
        }
    }while(fabs(fx0)>=1e-5);
    printf("x=%6.2f\n",x0);
    return 0;
}

or

#include <stdio.h>
#include <math.h>
int main()
{
    // find the root of the equation 2x^3 - 4x^2 + 3x - 6 = 0
    // search by dichotomy in [-10, 10]
    float x0,x1,x2,fx0,fx1,fx2;
    int step = 0;
    do
    {
        printf("enter x1 & x2:");
        scanf("%f,%f",&x1,&x2);
        fx1=x1*((2*x1-4)*x1+3)-6;
        fx2=x2*((2*x2-4)*x2+3)-6;
    }while(fx1*fx2>0);
    
    while(step<100){
        step++;
        x0=(x1+x2)/2;
        fx0=x0*((2*x0-4)*x0+3)-6;
        if((fx0*fx1)<0){
            x2=x0;
            fx2=fx0;
        }
        else{
            x1=x0;
            fx1=fx0;
        }
        if(fabs(fx0)<1e-5) break;
    }
    printf("x=%6.2f\n",x0);
    printf("It runs %d steps\n",step);
    return 0;
}
  • Find the root of the equation \(2x^3 - 4x^2 + 3x - 6 = 0\). Search by Newton’s method
#include <stdio.h>
#include <math.h>
int  main()
{
    // find the root of the equation 2x^3 - 4x^2 + 3x - 6 = 0
    // search by Newton method    
    double x1,x0,f,f1;
    x1=0;
    do
    {
        x0=x1;
        f=((2*x0-4)*x0+3)*x0-6;
        f1=(6*x0-8)*x0+3;
        x1=x0-f/f1;
    }while(fabs(x1-x0)>=1e-5);
    printf("The root of equation is %5.2f\n",x1);
    return 0;
}

or

#include <stdio.h>
#include <math.h>
#define MAX_STEP 100
int  main()
{
    // find the root of the equation 2x^3 - 4x^2 + 3x - 6 = 0
    // search by Newton method    
    int step = 0;
    x0 = 0.0; 
    while(step<MAX_STEP){
        step++;
        f=((2*x0-4)*x0+3)*x0-6;
        f1=(6*x0-8)*x0+3;
        x1=x0-f/f1;
        if(fabs(x1-x0)<1e-5) break;
        x0 = x1;
    }
    printf("The root of equation is %5.2f\n",x1);
    printf("It runs %d steps\n",step);   
    return 0;
}
  • Minimize the function \(f(x) = 15e^(x/2) + 3x^2 - 2x - 2\). Seach by dichotomy around the symmetric axis of the quadratic function.
#include <stdio.h>
#include <math.h>
#define EPS 1e-6
#define MAX_STEP 100
#define OBJfun(x) 15*exp(x/2) + 3*x*x - 2*x - 2
#define GRAD(x) 7.5*exp(x/2) + 6*x - 2

int main(){
    // minimize the function 5e^(x/2) + 3x^2 - 2x - 2
    // search by dichotomy around the symmetric axis of the quadratic function.
    int step=0;
    double f1, f0, x0, x1,width = 5.0, xhalf,fhalf, a2b;
    a2b = 2.0/6;
    x0 = a2b - width;
    x1 = a2b + width;
    f0 = GRAD(x0);
    f1 = GRAD(x1);
    printf("f0 = %f,\t f1 = %f\n",f0,f1);
    while(step<MAX_STEP){
        step++;
        xhalf=(x0+x1)/2;
        fhalf=GRAD(xhalf);
        if((fhalf*f0)<0){
            x1=xhalf;
            f1=fhalf;
        }
        else{
            x0=xhalf;
            f0=fhalf;
        }
        if(fabs(fhalf)<1e-5) break;
    }
    printf("%lf arrives at the minimal at %lf.\n",OBJfun(xhalf),xhalf);
    printf("It runs %d steps\n",step);
    return 0;
}
  • Minimize the function \(f(x) = 15e^(x/2) + 3x^2 - 2x - 2\). Seach by Newton’s method
#include <stdio.h>
#include <math.h>
#define EPS 1e-6
#define MAX_STEP 100
double objfun(double x, int d){
    double f;
    switch(d){
        case 0: f = 15*exp(x/2) + 3*x*x - 2*x - 2; break;
        case 1: f = 7.5*exp(x/2) + 6*x - 2; break;
        case 2: f = 3.75*exp(x/2) + 6; break;
        default: printf("\a");
    }
    return f;
}
int main(){
    // minimize the function f(x) = 15e^(x/2) + 3x^2 - 2x - 2
    // search by Newton method

    // g(x) = 0 ------------------- grad = df(x)
    // g1(x) = dg(x) -------------- hess = d^2f(x)
    // x1 = x0 - g(x0)/g1(x0) ----- x1 = x0 - grad/hess

    int step=0;
    double min0 = 100.0, min1, x0 = 5.0, x1, grad, hess;
    while(step<MAX_STEP){
        step++;
        grad = objfun(x0,1);
        hess = objfun(x0,2);
        x1 = x0 - grad/hess;
        min1 = objfun(x1,0);
        if(fabs(min1-min0)<EPS) break;
        x0 = x1;
        min0 = min1;
    }
    printf("%lf arrives at the minimal at %lf.\n",min1,x1);
    printf("It runs %d steps\n",step);
    return 0;
}

or

#include <stdio.h>
#include <math.h>
#define EPS 1e-6
#define MAX_STEP 100
double objfun(double x, int d){
    double f;
    if(d==0) f = 15*exp(x/2) + 3*x*x - 2*x - 2;
    else if(d==1) f = 7.5*exp(x/2) + 6*x - 2;
    else if(d==2) f = 3.75*exp(x/2) + 6;
    else printf("\a");
    return f;
}
int main(){
    // minimize the function f(x) = 15e^(x/2) + 3x^2 - 2x - 2
    // search by Newton method

    // g(x) = 0 ------------------- grad = df(x)
    // g1(x) = dg(x) -------------- hess = d^2f(x)
    // x1 = x0 - g(x0)/g1(x0) ----- x1 = x0 - grad/hess

    double x0 = 5.0, x1;
    for(int step=1;step<MAX_STEP;step++){
        x1 = x0 - objfun(x0,1)/objfun(x0,2);        
        if(fabs(x1-x0)<EPS) break;
        x0 = x1;
    }    
    printf("%lf arrives at the minimal at %lf.\n",min1,x1);
    printf("It runs %d steps\n",step);
    return 0;
}

2 Problem in the previous week

  • Decrypt a sentence “Rmj lwjfyjxy Atrrzsnxy Nfwy~ tk Amnsf!”
  • Decryption rule:
    • print the origin character plus 2 if the origin character is between 63 and 88
    • print the origin character minus 5 if the origin character is between 102 and 127
    • print the origin character otherwise
#include <stdio.h>
#include <string.h>
int main(){
    // encryption/decryption
    // encryption rule:
    // print the origin character minus 2 if the origin character is capital
    // print the origin character plus 5 if the origin character is lowercase
    // print the origin character otherwise
    //  
    // The decryption: "Rmj lwjfyjxy Atrrzsnxy Nfwy~ tk Amnsf!"
    // decoded rule:
    // print the origin character plus 2 if the origin character is between 63 and 88
    // print the origin character minus 5 if the origin character is between 102 and 127
    // print the origin character otherwise
    int i,N;
    char encodes[] = "Rmj lwjfyjxy Atrrzsnxy Nfwy~ tk Amnsf!";
    N = strlen(s);
    char encodes[N+1];

    printf("\n\nThe decypted is \n");
    for(i=0;i<N;i++){
        if(encodes[i]>=63&&encodes[i]<=88){
            decodes[i] = encodes[i] +2;
            printf("%c", decodes[i]);
        }
        else if(encodes[i]>=102&&encodes[i]<=127){
            decodes[i] = encodes[i]-5;
            printf("%c", decodes[i]);
        }
        else{
            decodes[i] = encodes[i];
            printf("%c", decodes[i]);
        }
    }
    decodes[N] = '\0';
    printf("\n%s",decodes);
    return 0;
}

or

#include <stdio.h>
#include <string.h>
int main(){
    // encryption/decryption
    // encryption rule:
    // print the origin character minus 2 if the origin character is capital
    // print the origin character plus 5 if the origin character is lowercase
    // print the origin character otherwise
    //  
    // The decryption: "Rmj lwjfyjxy Atrrzsnxy Nfwy~ tk Amnsf!"
    // decoded rule:
    // print the origin character plus 2 if the origin character is between 63 and 88
    // print the origin character minus 5 if the origin character is between 102 and 127
    // print the origin character otherwise
    int i,N;
    char s[] = "The greatest Communist Party of China!";
    N = strlen(s);
    printf("The encrypted is \t");
    char encodes[N+1], decodes[N+1];
    for(i=0;i<N;i++){
        if(s[i]>='A'&&s[i]<='Z'){
            encodes[i] = s[i]-2;
            printf("%c", s[i]-2);
        }
        else if(s[i]>='a'&&s[i]<='z'){
            encodes[i] = s[i]+5;
            printf("%c", s[i]+5);
        }
        else{
            encodes[i] = s[i];
            printf("%c", s[i]);
        }

    }

    printf("\n\nThe decypted is \t");
    for(i=0;i<N;i++){
        if(encodes[i]>=63&&encodes[i]<=88){
            decodes[i] = encodes[i] +2;
            printf("%c", decodes[i]);
        }
        else if(encodes[i]>=102&&encodes[i]<=127){
            decodes[i] = encodes[i]-5;
            printf("%c", decodes[i]);
        }
        else{
            decodes[i] = encodes[i];
            printf("%c", decodes[i]);
        }
    }

    decodes[N] = '\0';
    printf("\n%s",decodes);
    return 0;
}

3 Problems

  1. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

  2. Calculate the sum of the even-valued terms of Fibonacci series. Starting with 1 and 2.