Arithmetic mean of prime Fibonacci Numbers up to the x Fibonacci Number
Python 2, 71 bytes
s=2.;a=b=c=1
exec"p=2**a%a==2;c+=p;s+=a*p;a,b=b,a+b;"*input()
print s/c
Try it online!
Python doesn't have useful arithmetic built-ins for this, so we do things by hand. The code iterates through Fibonacci numbers via a,b=b,a+b
starting from a=b=1
.
The Fermat primality test with base 2 is used to identify primes as a
where 2^a == 2 (mod a)
. Though this only checks for probable primes, none of the false positives are within the first 40 Fibonacci numbers.
The current sum s
and count c
of primes are updated each time a prime is encountered, and their ratio (the mean) is printed at the end. Because the prime check misses a=2
and it's guaranteed to be in the input range, the sum starts at 2 and the count starts at 1 to compensate.
Jelly, 11 bytes
ÆḞ€ÆPÐfµS÷L
Try it online!
How it works
ÆḞ€ÆPÐfµS÷L Main link. Argument: n (integer)
ÆḞ€ Map the Fibonacci atom over [1, ..., n].
ÆPÐf Filter by primality.
µ Begin a new chain. Argument: A (array of Fibonacci primes)
S Yield the sum of A.
L Yield the length of A.
÷ Compute the quotient.
Mathematica, 38 bytes
Mean@Select[Fibonacci@Range@#,PrimeQ]&
(* or *)
Mean@Select[Fibonacci~Array~#,PrimeQ]&
Explanation
Mean@Select[Fibonacci@Range@#,PrimeQ]&
& (* For input # *)
Range@# (* List {1, 2, 3, ... #} *)
Fibonacci@ (* Find the corresponding fibonacci numbers *)
Select[ ,PrimeQ] (* Select the prime terms *)
Mean@ (* Take the Mean *)