| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Vyxal 3 | 250617T113152Z | Themooni |
| 071 | Whispers v3 | 201105T165700Z | caird co |
| 007 | Jelly | 201105T163900Z | caird co |
| 012 | Japt x | 170705T170110Z | Shaggy |
| 006 | Husk | 201017T013924Z | LegionMa |
| 113 | JavaScript ES6 | 170419T093711Z | Shaggy |
| 104 | Axiom | 170417T082951Z | user5898 |
| 218 | Prolog SWI | 170304T192748Z | Adnan |
| 013 | 05AB1E | 170308T112204Z | Emigna |
| 093 | Röda | 170304T194730Z | fergusq |
| 071 | Python 2 | 170304T184435Z | xnor |
| 129 | dc | 170305T040031Z | R. Kap |
| nan | 170305T011719Z | Brad Gil | |
| 010 | Actually | 170305T004417Z | Adnan |
| 091 | perl | 170305T003737Z | Kjetil S |
| 038 | Mathematica | 170304T180844Z | JungHwan |
| 011 | Pyke | 170304T203347Z | Blue |
| 016 | MATL | 170304T181922Z | Luis Men |
| 049 | Maxima | 170304T184616Z | rahnema1 |
| 071 | Octave | 170304T183342Z | Luis Men |
| 011 | Jelly | 170304T175541Z | Dennis |
Vyxal 3, 10 bytes
k±⎄+}⊖~℗∆A
k±⎄+}⊖~℗∆A
k±⎄+} # fibonacci infinite generator:
k± # [1,1]
⎄+} # generate infinite list using addition
⊖ # take the first [implicit input]
~℗ # filter by primes
∆A # arithmetic mean
💎
Created with the help of Luminespire.
<script type="vyxal3">
k±⎄+}⊖~℗∆A
</script>
<script>
args=[["10"],["15"],["20"],["11"],["30"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Whispers v3, 71 bytes
> Input
> fₙ
>> 2ᶠ1
>> L’
>> Select∧ 4 3
>> mean(5)
>> Output 6
Unfortunately, no TIO as this relies on v3 features, and v3 isn't on TIO. However, you can tell it by downloading and running the interpreter in the linked GitHub repo
How it works
> Input
Here, we "define" line 1 to hold our input integer, n
> fₙ
On line 2, we store an infinite list of Fibonacci numbers
>> 2ᶠ1
We then use the ᶠ dyad to take the ᶠirst n elements from the infinite list
>> L’
This line is a "functional operator line". It is passed a value on the Left and returns the ’ (prime) operator result on that value
>> Select∧ 4 3
We now take the list of Fibonacci numbers and Select those which return true when run through line 4
>> mean(5)
>> Output 6
Finally, we take the mean of the resulting list, before outputting it
Jelly, 7 bytes
ÆḞ€ẒƇÆm
The newest version of Jelly really helps with this. This is essentially identical to Dennis' answer but uses the newer features, so go upvote his answer.
How it works
ÆḞ€ẒƇÆm - Main link. Takes n on the left
ÆḞ€ - i'th Fibonacci number for each i in [1, 2, .., n]
Ƈ - Comb; Keep those for which the following is true:
Ẓ - Is prime?
Æm - Take the arithmetic mean
Husk, 6 bytes
Afṗ↑İf
Try it online! Outputs the mean as an exact rational number.
Explanation
Afṗ↑İf (Let X denote the argument.)
A Take the arithmetic mean of
↑ the first X values of
İf the infinite list of Fibonacci numbers,
fṗ filtered for primality.
JavaScript ES6, 137 136 118 113 bytes
m=
n=>(a=[],(f=x=>a[x]=x<2?x:f(--x)+f(--x))(n),eval((a=a.filter(x=>eval("for(y=x;x%--y;);y==1"))).join`+`)/a.length)
console.log(m(10))
console.log(m(15))
console.log(m(20))
console.log(m(11))
console.log(m(30))
History
118 bytes
n=>(a=[0,1,1],(f=x=>a[--x]=a[x]||f(x)+f(--x))(n),eval((a=a.filter(x=>eval("for(y=x;x%--y;);y==1"))).join`+`)/a.length)
136 bytes
n=>(a=[0,1,1],(f=x=>a[--x]=a[x]||f(x)+f(--x))(n),eval((a=a.filter(x=>x>1&&!Array(x).fill().some((i,j)=>j>1&&!(x%j)))).join`+`)/a.length)
137 bytes
n=>(a=[0,1,1],(f=x=>a[--x]=a[x]||f(x)+f(--x))(n),eval((a=a.filter(x=>{d=x;while(--d>1)if(!(x%d))return 0;return x>1})).join`+`)/a.length)
Axiom, 104 bytes
g(n)==(y:=x:=r:=0;repeat(x>n=>break;j:=fibonacci(x);x:=x+1;if prime?(j)then(r:=r+j;y:=y+1));y=0=>-1;r/y)
ungolfed, test code and results
f(n)==
y:=x:=r:=0
repeat
x>n=>break
j:=fibonacci(x)
x:=x+1
if prime?(j) then(r:=r+j;y:=y+1)
y=0=>-1
r/y
(3) -> [[i,g(i)::Float] for i in [1,2,3,10,15,20,11,30,50]]
Compiling function g with type PositiveInteger -> Fraction Integer
(3)
[[1.0,- 1.0], [2.0,- 1.0], [3.0,2.0], [10.0,5.75], [15.0,57.5],
[20.0,277.4285714285 7142857], [11.0,22.4], [30.0,60536.4444444444 44444],
[50.0,309568576.1818181818 2]]
i try to duplicate the matematica, octave etc languages entry, if one not count the mean() function, to implement it would be here 62 bytes so good too
mean(a:List Float):Any==
i:=1; s:=0.0
repeat
if~index?(i,a)then break
s:=s+a.i
i:=i+1
i=1=>[]
s/(i-1)
--62 bytes
f(x:NNI):Any==mean(select(prime?,[fibonacci(i)for i in 1..x]))
Prolog (SWI), 269 264 254 218 bytes
- Thanks to Fatalize for saving 37 bytes!
- Thanks to Emigna for saving 9 bytes!
I'm fairly certain that I could golf some more bytes down.
X/X:-X<3.
N/R:-A is N-1,B is N-2,A/C,B/D,R is C+D.
2^0^0.
C^S^E:-G is C-1,G/M,G^Q^R,(p(M)->S=M+Q,E is R+1;S=Q,E is R).
a(X,S):-X^R^Q,S is R/Q.
p(N):-N*(N-1).
p(2).
p(3).
N*2:-N mod 2=\=0.
N*C:-N mod C=\=0,D is C-1,N*D.
Run it as a(15, R). for x = 15, R is the output variable.
A more readable version:
fibonacci(1, 1) :- !.
fibonacci(2, 2) :- !.
fibonacci(N, R) :-
N0 is N - 1,
N1 is N - 2,
fibonacci(N0, R1),
fibonacci(N1, R2),
R is R1 + R2.
listed(2, 0, 0) :- !.
listed(C, S, Successes) :-
C0 is C - 1,
fibonacci(C0, Result),
listed(C0, S0, S1),
(
is_prime(Result)
->
S = Result + S0, Successes is S1 + 1
; S = S0, Successes is S1
).
f(X, S) :-
listed(X, R, Q),
S is R / Q.
is_prime(Num) :- is_prime(Num, Num - 1).
is_prime(2).
is_prime(3).
is_prime(Num, 2) :- Num mod 2 =\= 0, !.
is_prime(Num, C) :-
Num mod C =\= 0,
C0 is C - 1,
is_prime(Num, C0).
05AB1E, 13 bytes
!ÅF¹£DpÏDOsg/
Try it online! or as a Test suite
Explanation
!ÅF # get a list of fibonacci numbers up to fac(input)
¹£ # take the first input elements of that list
D # duplicate
p # isprime on the copy
Ï # keep the fibonacci numbers which are true in the isprime list
D # duplicate the list of fibonacci primes
O # sum the list
s # swap the other copy to the top of the stack
g # get its length
/ # divide sum by length
Röda, 98 94 93 bytes
f n{z=0;i=1;j=1;s=0;seq 2,n-1|{|_|c=i+j;i=j;j=c;seq 2,c-1|{z+=1;s+=c}if[c%p>0]()for p}_[s/z]}
It's a function that returns the result as a floating point number.
Ungolfed version:
function f(n) {
i = 1
j = 1
sum = 0
num = 0
seq(2, n-1) | for _ do
/* calculate next fibonacci number: */
c = i+j
i = j
j = c
/* if it's prime: */
{
num += 1
sum += c
/* primality test: */
} if [c%p > 0]() for p in [seq(2, c-1)]
done
return sum/num
}
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
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.
dc, 129 bytes
?sa0sb1sd0ss0sz[[lF1+sF]sqlelG!=q]si[ls+sslz1+sz]sw[lddlb+dsdrsbdsG2se0sF[lGdle%0=ile1+dse<p]dspxlF0=wla1-dsa1<c]dscxEkls1-lz1-/p
A Fibonacci number generator and primality checker all in one. Nice.
Perl 6, 51 bytes
{sum($/=[grep *.is-prime,(0,1,*+*...*)[0..$_]])/$/}
Expanded:
{
sum(
$/ = [ # store as an Array in $/
grep
*.is-prime, # find the primes
(
0, 1, *+* ... * # Fibonacci sequence
)[ 0 .. $_ ] # grab the indicated values in the sequence
]
)
/
$/ # use the Array as a number (elems)
}
Actually, 10 bytes
Code:
R♂F;Mr♂P∩æ
Explanation:
R # On the input, create the range [1 .. input].
♂F # Map the nth Fibonacci command over it.
;M # Duplicate and get the maximum of the list.
r # Create the range [0 .. maximum - 1].
♂P # Map the nth prime operator over each element (zero-indexed).
∩ # Intersection of both.
æ # Get the mean and implicitly display.
Uses the CP-437 encoding. Try it online!
perl, 91 bytes
$b=1;map{($a,$b)=($b,$a+$b),$p=("x"x$b)!~/^(.|(..+)\2+)$/,$s+=$b*$p,$c+=$p}2..$x;print$s/$c
Cuold have been 8 bytes shorter if the modulo pseudoprime test worked as well in perl as in the python answer:
$b=1;$s=2;map{($a,$b)=($b,$a+$b),$p=2**$b%$b==2,$s+=$b*$p,$c+=$p}2..$x;print$s/++$c
...but this gives wrong answer for input > 16 in perl.
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 *)
MATL, 16 bytes
lOi:"yy+]vtZp)Ym
Explanation
lO % Push 1, then 0. This way the generated numbers with indices 1, 2, 3, ...
% will be 1, 1, 2, ... as required
i % Input n
:" % Do the following n times
yy % Duplicate top two numbers
+ % Add
] % End
v % Concatenate all numbers into a column vector
t % Duplicate
Zp % Vector of logical values: true for prime numbers, false otherwise
) % Apply that vector as an index. This keeps only prime numbers
Ym % Mean. Implicitly display
Maxima, 49 bytes
f(n):=mean(sublist(makelist(fib(x),x,n),primep));
Octave, 75 71 bytes
@(n)mean((t=fix(((1+(s=5^.5)).^(x=1:n)-(1-s).^x)/s./2.^x))(isprime(t)))
Anonymous function that uses Binet's formula. Input and output are in the form of function arguments.
Jelly, 11 bytes
ÆḞ€ÆPÐfµS÷L
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.