#include <stdio.h>
#include <time.h>
// 函数声明
long long sum_leijia(long long a, long long b);
long long sum_gaosi(long long a, long long b);

int main() {
    clock_t before, middle, after;
    long long a = 1LL, b = 100000000LL;
    long long result1 = 0LL, result2 = 0LL;     
    before = clock();    
    result1 = sum_leijia(a, b);
    middle = clock();    
    printf("累加计算结果: %lld, 所用时间: %lf 毫秒\n", result1, (double)(middle - before) / CLOCKS_PER_SEC * 1000);
    
    // 计算公式方式的和并计时
    result2 = sum_gaosi(a, b);
    after = clock();    
    printf("高斯求和结果: %lld, 所用时间: %lf 毫秒\n", result2, (double)(after - middle) / CLOCKS_PER_SEC * 1000);    

    return 0;
} 
long long sum_leijia(long long a, long long b) {
    long long sum = 0LL;
    for (long long i = a; i <= b; i++) {
        sum += i;
    }
    return sum;
}
 
long long sum_gaosi(long long a, long long b) {
    return (a + b) * (b - a + 1) / 2;
}
