How does the proof of work of Primecoin actually work?

I ike Primecoin, however, I do not understand how the proof of work works.

Say I see the current blopckchain. I am a miner. I see the next block of pending transactions. What do I do? How, exactly, do I produce a proof of work that validates the pending transactions as well as finds a chaib of cunningham primes?

Relevant soure code if avilable would also be good.

Thanks!

when mining primecoin you are looking for prime number chains, Cunningham of the first kind, Cunningham of the second kind and Bi-twin chains are accepted. Finding the prime number chains becomes exponentially harder as the chain length is increased (difficulty).

I understand that the proof of work is about finding chains of primes.

My question is: given a blockchain state, what is the problem I need to solve?

Are you saying that the problem of finding the chain of primes is independent of the transactions in the blockchain?

yes,
transactions are broadcasted on the network, peers collect transactions, when someone finds a block the transactions are added to it

So what has finding a chain of primes got to do with the specific transactions on the network? Is it completely independent?

Yes, the finding a chain of primes has nothing to do with those transactions.
blocks dont require transactions (except for the coinbase transaction generating the blockreward), transactions do require blocks, so I guess the only relation with finding a chain and the transactions is that the transactions get recorded on the chain when there is a block.

I see.

So how does Primecoin decide which blocks are valid? How does it resolve forks? Is it the miner who finds a chain of primes is the one that gets to decide the direction the blockchain goes? Or is there hashing like in bitcoin?