1 Sum of two large integers

  • Find the sum of two large integers, which are storied in the arrays.
// The sum of two large integers
#include <stdio.h>
#include <stdlib.h>
#define N 100

void printArrayInt(int *arr, int n, int ncol){
    int i;
    for (i=0; i<n; i++){
        printf("%d   ",arr[i]);
        if(!((i+1)%ncol)) printf("\n");
    }
    printf("\n");
}

void printArrayInverseInt(int *arr, int n, int ncol){
    int i;
    for (i=0; i<n; i++){
        printf("%d   ",arr[n-1-i]);
        if(!((i+1)%ncol)) printf("\n");
    }
    printf("\n");
}

int add(int *a, int *b, int curr_len, int s){
    //add b to a
    int i,temp,temp1=0;
    for(i=0;i<curr_len;i++){
        temp = a[i+s]+b[i]+temp1;
        a[i+s]=temp%10;
        temp1 = (int)(temp/10);
    }
    if(temp1>0){
        a[s+curr_len]=temp1;
        curr_len++;
    }
    return curr_len+s;
}
int main(){
    int i,len,a[N+1], b[N];
    srand(1);
    for (i=0;i<N;i++){
        a[i] = rand() % 10;
        b[i] = rand() % 10;
    }
    while(!a[N-1]) a[N-1] = rand() % 10;
    while(!b[N-1]) b[N-1] = rand() % 10;
    printArrayInverseInt(a,N,25);
    printArrayInverseInt(b,N,25);
    len = add(a,b,N,0);
    printArrayInverseInt(a,len,25);
    printf("length of a is %d\n",len);
    return 0;
}

2 Aother Example

  • 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

  • More generally, what is the smallest positive number that is evenly divisible by all of the numbers from 1 to N? Here N could be large, such as 10000.

  • Refer to the Section “Loops continue and arrays” and “Arrays”.

#include<stdio.h>
#include<math.h>
#define N 100
#define M 1000
int isPrime(int n){
    int i=2;
    double sqrtn=sqrt(1.0*n);
    while(i<=sqrtn){
        if(!(n%i)) return 0;
        i++;
    }
    return 1;
}

int product(int fact[], int n, int curr_len, int digits){
    int i,temp1=0;
    curr_len+=digits-1;
    for(i=0;i<curr_len;i++){
        temp1=fact[i]*n+temp1/10;
        fact[i]=temp1%10;
    }
    while(temp1/=10){
        fact[i]=temp1;
        curr_len++;
    }
    return curr_len;
}

int isDivededBy1toN(int fact[],int n){
    int i,j,m0;
    int curr_len=1,digits,m;
    for(i=2;i<=n;i++)
        if(isPrime(i)){
            m0=j=i;
            while((j*=i)<=n) m0 = j;
            digits = 1;
            m=m0;
            while(m/=10)  digits++;
            curr_len = product(fact,m0,curr_len,digits);
            printf("j=%d\n",m0);
        }
    return curr_len;
}

int main(){
    int i,fact[M],curr_len;
    for(i=0;i<M;i++) fact[i]=0;
    fact[0]=1;
    curr_len = isDivededBy1toN(fact,N);
    for(i=curr_len-1;i>=0;i--) printf("%d",fact[i]);

    return 0;
}

3 Problem in the previous week

  • A magic square is a square grid (where n is the number of cells on each side) filled with distinct positive integers in the range. such that each cell contains a different integer and the sum of the integers in each row, column and diagonal is equal, where the sum equals \(n(n^2+1)/2\).
// A magic square is a square grid (where n is the number of cells on each side) 
// filled with distinct positive integers in the range. 
// such that each cell contains a different integer 
// and the sum of the integers in each row, column and diagonal is equal,
// where the sum equals (n^2+1)*n/2.
#include<stdio.h>
#define n 11 
//n must be odd.
int main(){
    int i,l,k,flag,half;
    int a[n][n],mid;
    half=(n-1)/2;   
    mid=(n*n+1)/2;
    a[half][half]=mid;
    for(i=1;i<half+1;i++){
        for(l=0;l<2;l++){
            flag=1;
            if(l%2==1) flag=-1;
            a[half-i*flag][half] = mid+(2*i*(i+1)-2*i+2)*flag;
            a[half][half-i*flag] = mid+(2*i*(i+1)-2*i)*flag;
            a[half-i*flag][half-i*flag] = mid-(2*i*(i+1)-2*i+1)*flag;
            a[half-i*flag][half+i*flag] = mid-(2*i*(i+1)-2*i-1)*flag;
        }

        for(l=1;l<i;l++){
            for(k=0;k<2;k++){
                flag=1;
                if(k%2==1) flag=-1;
                a[half-i*flag][half-i+l] = mid+(2*i*(i+1)-(l-1)*2)*flag;
                a[half-i*flag][half+l] = mid-(2*i*(i+1)-2*i-2-(l-1)*2)*flag;
                a[half-i+l][half-i*flag] = mid-(2*i*(i+1)-1-(l-1)*2)*flag;
                a[half+l][half-i*flag] = mid+(2*i*(i+1)-2*i-3-(l-1)*2)*flag;
            }
        }
    }

    printf("The a[%d][%d] are :\n\n",n,n);
    for(i=0;i<n;i++){
        for(k=0;k<n;k++)
            printf("%6d",a[i][k]);
        printf("\n");
    }
    return 0;
}
// Calculate the first 50 pairs (p,q) of Pell equation.
#include<stdio.h>
void pf2(int *a1, int *b1, int *Lab, int N){
    int i,La,ta,tb,ta1,tb1,n=1;
    a1[0]=3;
    b1[0]=2;
    Lab[0]=Lab[1]=1;
    while(n<N){     
        ta=tb=0;La=Lab[0];
        for(i=0;i<La;i++){
            ta1=a1[i]*3+b1[i]*4+ta; 
            tb1=a1[i]*2+b1[i]*3+tb;
            a1[i]=ta1%10;   
            b1[i]=tb1%10;   
            ta=ta1/10;  
            tb=tb1/10;  
        }
        Lab[0]=La;
        Lab[1]=La;
        if(ta){
            a1[i]=ta;
            Lab[0]++;
        }
        if(tb){
            b1[i]=tb;
            Lab[1]++;
        }
        //printf("n=%d \t La=%d \t Lb = %d\n",n, Lab[0], Lab[1]);
        n++;
    }       
}

int main(){
    int i;  
    int a[100], b[100], Lab[2];
    for (i = 0; i < 100; i++) a[i]=b[i]=0;  
    pf2(a,b,Lab,50);
    
    printf("p=");
    for(i=Lab[0]-1;i>=0;i--) 
        printf("%d",a[i]);
    printf("   q=");
    for(i=Lab[1]-1;i>=0;i--)
        printf("%d",b[i]);
    printf("\n");
    
    return 0;
}