Here's some code in Swift that calculates the average of the logarithm of the first 100 primes:
main.swift590 chars29 lines
The isPrime
function tests whether a number is prime using a brute force method, checking all numbers from 2 up to n-1. The primes
function generates a list of the first n prime numbers.
We then map the log() function over each of the prime numbers to get their natural logarithm. Finally, we use the reduce
function to sum up the log values and divide by the total number of primes to get the average value of the logged primes.
gistlibby LogSnag