Averages/Arithmetic mean: Difference between revisions
Content added Content deleted
(Added Bracmat) |
|||
Line 228: | Line 228: | ||
^10+1g10+< |
^10+1g10+< |
||
Enter 0 (zero) to finish.</lang> |
Enter 0 (zero) to finish.</lang> |
||
=={{header|Bracmat}}== |
|||
Here are two solutions. The first uses a while loop, the second scans the input by backtracking. |
|||
<lang bracmat> |
|||
(mean1= |
|||
sum length n |
|||
. 0:?sum:?length |
|||
& whl |
|||
' ( !arg:%?n ?arg |
|||
& 1+!length:?length |
|||
& !n+!sum:?sum |
|||
) |
|||
& !sum*!length^-1 |
|||
); |
|||
(mean2= |
|||
sum length n |
|||
. 0:?sum:?length |
|||
& !arg |
|||
: ? |
|||
( #%@?n |
|||
& 1+!length:?length |
|||
& !n+!sum:?sum |
|||
& ~ |
|||
) |
|||
? |
|||
| !sum*!length^-1 |
|||
); |
|||
</lang> |
|||
To test with a list of all numbers 1 .. 999999: |
|||
<lang> |
|||
( :?test |
|||
& 1000000:?Length |
|||
& whl'(!Length+-1:?Length:>0&!Length !test:?test) |
|||
& out$mean1$!test |
|||
& out$mean2$!test |
|||
)</lang> |
|||
=={{header|Brainf***}}== |
=={{header|Brainf***}}== |
||
Line 233: | Line 270: | ||
This code is an infinite loop if the average isn't a whole number. I don't have the time, can someone fix it to work with all numbers, not just single-digit numbers? |
This code is an infinite loop if the average isn't a whole number. I don't have the time, can someone fix it to work with all numbers, not just single-digit numbers? |
||
<lang bf>>,[-<+>>+<],<[->-<]>[-->+<]>.</lang> |
<lang bf>>,[-<+>>+<],<[->-<]>[-->+<]>.</lang> |
||
=={{header|Brat}}== |
=={{header|Brat}}== |