Basic math problems

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43460822/article/details/98593189

The learning content:
Here Insert Picture Description
1, judgment leap year
leap year rule: four years a leap, a hundred years does not run, then run four hundred years
given algorithm:

public class LeapYearQuestion {
	static int isLeapYear(int year){
		if((year%4==0)&&(year%100!=0)||year%400==0){
			return 1;
		}else{
			return 0;
		}
	}
	public static void main(String[] args){
		System.out.print("输出2000~3000的所有闰年\n");
		int count=0;
		for(int i=2000;i<3000;i++){
			if(isLeapYear(i)==1){
				System.out.print(i+"\t");
				count++;
				if(count%10==0){
					System.out.println("\n");
				}
				
			}
		}
	}

}

2. polynomial

2.1 one-dimensional polynomial evaluator

A polynomial of the general computing recursive algorithm methods can be used. First, the polynomial can be transformed into the following equivalent form:

P (X) = (... ((. 1 AN- X-AN 2 + X) * X +-AN. 3) + ... + A1 X) X + A0
can be seen from this expression, as long as one layer from the inside out in accordance with the following recursive manner, the entire value can be calculated to obtain a one-dimensional polynomial.
; = RN-AN. 1. 1-
Rk = Rk +. 1
+ X AK;
K = n--2, ..., 1,0
latter layer a layer is calculated, the value of Ro is obtained by the polynomial P (x) of.
One-dimensional polynomial evaluation algorithm:

static double po1D(double a[],int n,double x){
		double result = a[n-1];
		for(int i=n-2;i>=0;i--){
			result=result*x+a[i];
		}
		return result;
	}

Example code:



public class Polynomial1D {
	static double po1D(double a[],int n,double x){
		double result = a[n-1];
		for(int i=n-2;i>=0;i--){
			result=result*x+a[i];
		}
		return result;
	}

	
	public static void main(String[] args) {
		double[] a={-15,-7,7,2,-3,7,3};
		double[] x={-2.0,-1,0,1,2,2.5};
		for(int i=0;i<x.length;i++){
			System.out.println("当x="+x[i]+"时,p(x)="+po1D(a,7,x[i]));
		}
	}

}

Random method 3.1 java language

3.1.1, Math.random () method, a random number is generated between 0 and 1 double, we can multiply it by a certain number, for example, multiplied by 100, which is less than the random number 100.

3.1.2, Random class java.util package itself, we can create a Random objects to generate random numbers, which can generate a random integer randomly float, randomized double, random long.

3.1.2, System class has a currentTimeMillis () method, which returns from at 0:00:00 on January 1, 1979 to present a number of milliseconds, the return type is long, we can take it as a random number.

Random generating random using the following method:

public class Random {
	public static  void main(String[] args){
		java.util.Random r = new java.util.Random();
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				System.out.printf("%3d ",r.nextInt(100));
			}
			System.out.println();
		}
	}

}

3.2 Algorithm [0,1] uniformly distributed random numbers between the

First, setting a base base = 256.0, and two constants a = 17.0 and 189.0. Here, the base and generally an integral multiple of the base 2, the constants a and b can be freely taken empirically.
code show as below:

	static double rand(double[] a){
		double base,u,v,p,temp1,temp2,temp3;
		base=256.0;
		u=17.0;
		v=189.0;
		temp1 = (a[0])*u+v;
		temp2 = (int)(temp1/base);
		temp3 = temp1-temp2*base;
		a[0]=temp3;
		p=a[0]/base;
		return p;
	}

3.3 generating a random number range in any

Like the above-described method, but the required output bit configuration, the following is a detailed Code:
[m, n-] floating point random number between

public class Rand01 {
	static double rand(double[] a){
		double base,u,v,p,temp1,temp2,temp3;
		base=256.0;
		u=17.0;
		v=189.0;
		temp1 = (a[0])*u+v;
		temp2 = (int)(temp1/base);
		temp3 = temp1-temp2*base;
		a[0]=temp3;
		p=a[0]/base;
		return p;
	}
	
	public static void main(String[] args){
		double[] a= {5.0};
		int  m=50.0;
		int n=100.0;
		System.out.println("产生0~1的随机数如下:");
		for(int i=0;i<7;i++){
			System.out.printf("%d  ",m+(n-m)*rand(a));
		}

	}

}

3.4 Algorithm uniformly distributed random integer between [m, n] of

With the algorithm uniformly distributed random number between [0, 1] above, if you want to get an integral, very simple, as long as the result to rounding.
Example code:

public class Rand01 {
	static double rand(double[] a){
		double base,u,v,p,temp1,temp2,temp3;
		base=256.0;
		u=17.0;
		v=189.0;
		temp1 = (a[0])*u+v;
		temp2 = (int)(temp1/base);
		temp3 = temp1-temp2*base;
		a[0]=temp3;
		p=a[0]/base;
		return p;
	}
	public static void main(String[] args){
		double[] a= {5.0};
		int  m=50;
		int n=100;
		System.out.println("产生0~1的随机数如下:");
		for(int i=0;i<7;i++){
			System.out.printf("%d  ",m+(int)((n-m)*rand(a)));
		}

	}

}

4, complex operation

Complex arithmetic operations:



public class Plural {
	//加法
	static void cPlus(double a,double b,double c,double d, double[] e, double[] f){
		e[0]=a+c;
		f[0]=b+d;
	}
	//减法
	static void cJian(double a, double b, double c, double d, double[] e, double[] f){
		e[0]=a-c;
		f[0]=b-d;
	}
	//乘法
	static void cCheng(double a, double b, double c, double d, double[] e, double[] f){
		e[0]=a*c-b*d;
		f[0]=a*d+b*c;
	}
	//除法
	static void cShu(double a, double b, double c, double d, double[] e, double[] f){
		double sq;
		sq = c*c+d*d;
		e[0]=(a*c+b*d)/sq;
		f[0]=(b*c-a*d)/sq;
	}
	static void cPowN(double a, double b, int n, double[] e, double[] f){
		double result;
		int i=0;
		e[0]=a;
		f[0]=b;
		if(n==1){
			return ;
			
		}else{
			for(i=1;i<n;i++){
				cCheng(e[0],f[0],a,b,e,f);
			}
		}
	}
	public static void main(String[] args){
		double a,b,c,d;
		double[] e = {0.0};
		double[] f = {0.0};
		a=4;b=6;
		c=2;d=-1;
		cPlus(a,b,c,d,e,f);
		System.out.println("( "+a+ "+"+b+"i"+") "+"+"+ "("+c+"+"+d+"i"+") ="+e[0]+"+"+f[0]+"i");
		cJian(a,b,c,d,e,f);
		System.out.println("( "+a+ "+"+b+"i"+") "+"-"+ "("+c+"+"+d+"i"+") ="+e[0]+"+"+f[0]+"i");
		cCheng(a,b,c,d,e,f);
		System.out.println("( "+a+ "+"+b+"i"+") "+"*"+ "("+c+"+"+d+"i"+") ="+e[0]+"+"+f[0]+"i");
		cShu(a,b,c,d,e,f);
		System.out.println("( "+a+ "+"+b+"i"+") "+"/"+ "("+c+"+"+d+"i"+") ="+e[0]+"+"+f[0]+"i");


		
		
		
	}

}

5, factorial

5.1 cycle factorial

 long fact(int n)
 {
 	int i;
 	long result=1;
 	for(i=1;i<=n;i++)
 	{
 		result*=i;
 	}
 	return result;
 	
 }

5.2 uses recursion to calculate the factorial

long fact(int n){
	if(n<=1)
		return 1;
	else
		return n*fact(n-1);
}

Guess you like

Origin blog.csdn.net/weixin_43460822/article/details/98593189