Calculation of difficulty is far easier than this. [...]
Here's the formula (simplified version, but it's close enough):
Difficulty * 2^32 / hash rate = time in seconds to solve a block
Since you know the perceived hash rate and the time it took to solve the block, you can calculate the difficulty.
Just to clarify - that's a (slightly simplified) formula for converting between a known difficulty and hash rate.
The difficulty changes based upon the block solve rate. Bitcoin wants to add a new block to the chain every 10 minutes. Every 2016 blocks it performs a sanity check and adjusts the network difficulty. The network says, "the previous 2016 blocks should have taken 2 weeks. How long did they actually take?" If the answer is, "it took longer than 2 weeks", the difficulty is adjusted downwards. If it took less than 2 weeks, the difficulty is adjusted upwards.
And
that code is here:
https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L53 (CalculateNextWorkRequired)
While this...
there's a modified taylor series calculation for the logarithm.
lots of code off the wiki
Is the code to get from the compact 'bits' form to a difficulty value. It's not used to determine the next difficulty.
I'm kind of wondering bitcoin isn't tied to any country with technology, economy, image, GDP, does it have real value so that the price can go up long term?
And that's a completely separate question along the lines of "What makes Bitcoin have value?".. a question you probably should have googled before