這里主要是講解一下Arduino的程序,使用了Arduino的編程語(yǔ)言朋魔,然后簡(jiǎn)單的寫(xiě)了一個(gè)求和函數(shù)函荣,如果你對(duì)數(shù)學(xué)的等差數(shù)列還不太了解的話,可以點(diǎn)擊查看相關(guān)的內(nèi)容了解什么是等差數(shù)列的定義及其求和公式浸踩!
這里利用了Arduino芯片作為一個(gè)處理器叔汁,然后跑一個(gè)程序,求出給定的一個(gè)數(shù)列检碗,并求出這個(gè)等差數(shù)列的前n項(xiàng)和据块!下面是總程序得截圖,如果你剛開(kāi)始看不懂的話折剃,沒(méi)關(guān)系另假,后面我會(huì)逐行講解其中的意思,以及這個(gè)程序還可以做怎樣的改進(jìn)怕犁!
/* Arduino Arithmetic progression
* this sketch demonstrate how to use Arduino to calculate a arithmetic progression
*? 2015/12/27
*/
int summation_traverse(int beginNumber, int n , int d)?
// 第一種方法求和边篮,其實(shí)也就是利用了遍歷的方法來(lái)求和的
// 其中beginNumber 是數(shù)列的第一個(gè)項(xiàng),n是要求和的前n項(xiàng)奏甫,
// d就是數(shù)列的公差
{
int temp = beginNumber;
int sum = 0;
for (int i = 0; i < n; i++) // 下面就是對(duì)前n項(xiàng)進(jìn)行求和了
{
sum = temp + sum; //每一次把一個(gè)數(shù)加起來(lái)戈轿,
//直到把所有的項(xiàng)都加完為止
temp = temp + d;//這個(gè)是第i個(gè)項(xiàng)
}
return sum;//然后把前n項(xiàng)和返回
}
int summation_arithmetic(int beginNumber, int n, int d) // second method , here use an formula calculate the sum
{
int sum = beginNumber * n + (n * (n - 1) * d) / 2;?
// it is the formula of sum
return sum;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.print("input a beginNumber and the n and the grade number d");
Serial.println("As an example if you input beginNumber is 10, and the n is 15, the grade number is 4");
Serial.print("the Sum result useing the summation_travse is:");
Serial.println(summation_traverse(10, 15, 4));
Serial.print("the sum result using the summation_arithmtic is:");
Serial.println(summation_arithmetic(10, 15, 4));
}
void loop() {
// put your main code here, to run repeatedly:
}