// $Id: moments.ici,v 1.0 2003/01/03 11:55:00 dada Exp $ // http://dada.perl.it/shootout // // contributed by Tim Long sum := 0.0; nums := array(); forall (f in gettokens(stdin, "\n", "")) { push(nums, f = float(f)); sum += f; } n := nels(nums); mean := sum / n; deviation := 0.0; average_deviation := 0.0; standard_deviation := 0.0; variance := 0.0; skew := 0.0; kurtosis := 0.0; forall (num in nums) { deviation = num - mean; average_deviation += abs(deviation); variance += (t := deviation * deviation); skew += (t *= deviation); kurtosis += (t *= deviation); } average_deviation /= n; variance /= (n - 1); standard_deviation = sqrt(variance); if (variance > 0.0) { skew /= n * variance * standard_deviation; kurtosis = kurtosis / (n * variance * variance) - 3.0; } sort(nums); mid := n / 2; if (n % 2 == 0) median = (nums[mid] + nums[mid - 1])/2; else median = nums[mid]; printf("n: %d\n", n); printf("median: %f\n", median); printf("mean: %f\n", mean); printf("average_deviation: %f\n", average_deviation); printf("standard_deviation: %f\n", standard_deviation); printf("variance: %f\n", variance); printf("skew: %f\n", skew); printf("kurtosis: %f\n", kurtosis);