new posts

This commit is contained in:
deguo
2023-08-12 03:23:37 +00:00
parent 543d0c1de7
commit 2722795115
4 changed files with 97 additions and 0 deletions

View File

@@ -7,6 +7,22 @@
"author": "jetstream0/Prussia",
"tags": ["code", "project", "web", "markdown", "typescript_javascript", "css"]
},
"fermats-little-theorem": {
"title": "Fermats Little Theorem",
"slug": "fermats-little-theorem",
"filename": "fermats_little_theorem",
"date": "12/08/2023",
"author": "jetstream0/Prussia",
"tags": ["code", "typescript_javascript", "math"]
},
"wikipedia-rabbitholes": {
"title": "Wikipedia Rabbitholes",
"slug": "wikipedia-rabbitholes",
"filename": "wikipedia_rabbitholes",
"date": "09/08/2023",
"author": "jetstream0/Prussia",
"tags": ["reading", "history", "wikipedia"]
},
"eve": {
"title": "Eve",
"slug": "eve",

View File

@@ -0,0 +1,35 @@
I read an article from [Quanta Magazine](https://www.quantamagazine.org/how-randomness-improves-algorithms-20230403/) that had this very interesting piece of information:
> The basic idea goes back to a result from the 17th-century French mathematician Pierre de Fermat, known as his "little theorem." Fermat considered two integers — call them `N` and `x`. He proved that if `N` is a prime number, then `x\^N x` is always a multiple of `N`, regardless of the value of `x`. Equivalently, if `x\^N x` is not a multiple of `N`, then `N` can't be a prime number. But the inverse statement isn't always true: If `x\^N x` is a multiple of `N`, then `N` is usually but not always prime.
> To turn Fermat's little theorem into a primality test, just take the `N` that you're interested in, choose `x` at random, and plug the two numbers into `x\^N x`. If the result is not a multiple of `N`, then you're done: You know that `N` is definitely composite. If the result is a multiple of N, then `N` is probably prime. Now pick another random `x` and try again. In most cases, after a few dozen tries, you can conclude with near certainty that `N` is a prime number. "You do this a small number of times," Blais said, "and somehow now your probability of having an error is less than the probability of an asteroid hitting the Earth between now and when you look at the answer."
It reminded me a little of zero knowledge proofs.
Anyways, using Fermat's Little Theorem, I wanted to create a little function that could see whether a number was a prime number or not. First, we had to have a number to check as one of the inputs. And since I was using Javascript, it probably should be a `BigInt`, so big inputs don't lose any precision. We probably also want to optionally let the caller specify how many checks to do.
Then, for each check, we can generate a random `x`, calculate `x\^N x`. If we call that, say, `m`, then we can do `m % N`, where `%` means modulo. If `m % N` is zero, that means `m` is a multiple of `N`, so we continue. If not, then we know the input is **not** a prime number, and can end it there. If we generate `x` many times, and `m % N` is always `0`, we can conclude with high probability that the input is a prime.
At first, I mistakenly did `N % m`, but that means `m` is a factor (not a *multiple*) of `N`, and `N` would be by definition, not a prime number (only factors of a prime number are itself and 1). I realised the problem pretty quickly.
After fixing that, this is the code I had:
```js
function is_prime(potential_prime, iterations=50) {
for (let i=0; i < iterations; i++) {
let x = BigInt(Math.floor(Math.random()*10000)); //0 =< x =< 9999
let m = x**potential_prime - x;
if (m%potential_prime !== BigInt(0)) {
return false;
}
}
return true;
}
```
I tested a random few of the [first 1000 prime numbers](https://en.wikipedia.org/wiki/List_of_prime_numbers#The_first_1000_prime_numbers), and some numbers that were not prime numbers, and it works! Yay!
Just to see if it worked, I tried putting in the very large prime number "531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127" as a test. And:
![](/images/prime_test.png)
...fair enough.

View File

@@ -0,0 +1,46 @@
Wikipedia articles. I like them. The "Did you know" and "On this day" sections on the front page are real treasure troves. They have archives too, so you'll never run out of articles to read.
Here's a very incomplete (and maybe actively updated) list of ones that led to more clicks and were interesting to read:
- [Second Anglo-Dutch War](https://en.wikipedia.org/wiki/Second_Anglo-Dutch_War)
- [Koxinga](https://en.wikipedia.org/wiki/Koxinga), rogue Ming loyalist general who defeated the Dutch and Qing, to rule Taiwan
- [Red Turban Rebellions](https://en.wikipedia.org/wiki/Red_Turban_Rebellions) and [Chinese Manichaeism](https://en.wikipedia.org/wiki/Chinese_Manichaeism) connected to each other through Manichaeian influence on the [White Lotus Society](https://en.wikipedia.org/wiki/White_Lotus)
- [List of Ethnic Groups in China](https://en.wikipedia.org/wiki/List_of_ethnic_groups_in_China)
- [Battle of Dien Bien Phu](https://en.wikipedia.org/wiki/Battle_of_Dien_Bien_Phu), where the Viet Minh kick out the French
- [Battle of Saigon (1955)](https://en.wikipedia.org/wiki/Battle_of_Saigon_%281955%29)
- [KHTML](https://en.wikipedia.org/wiki/KHTML), made by KDE, which suprisingly is the parent of both Chrome ([Blink](https://en.wikipedia.org/wiki/Blink_%28browser_engine%29)) and Safari ([Webkit](https://en.wikipedia.org/wiki/WebKit))
- [Syrian Civil War](https://en.wikipedia.org/wiki/Syrian_civil_war) and it's numerous factions, like the non-secular [Syrian Salvation Government](https://en.wikipedia.org/wiki/Syrian_Salvation_Government) rebels or the Kurdish [Rojava](https://en.wikipedia.org/wiki/Autonomous_Administration_of_North_and_East_Syria) (related: )
- [Circassian Genocide](https://en.wikipedia.org/wiki/Circassian_genocide), possibly the biggest genocide of the 19th century
- [Basmachi movement](https://en.wikipedia.org/wiki/Basmachi_movement), Central Asian rebellion against Soviet rule, with notable participant [Enver Pasha](https://en.wikipedia.org/wiki/Enver_Pasha), one of the [Three Pashas](https://en.wikipedia.org/wiki/Three_Pashas) who perpetrated the [Armenian Genocide](https://en.wikipedia.org/wiki/Armenian_genocide)
- [Saigo Takamori](https://en.wikipedia.org/wiki/Saig%C5%8D_Takamori), [Meiji Restoration](https://en.wikipedia.org/wiki/Meiji_Restoration) and [Satsuma Rebellion](https://en.wikipedia.org/wiki/Satsuma_Rebellion) leader
- [Shimabara Rebellion](https://en.wikipedia.org/wiki/Shimabara_Rebellion), Christian rebellion in Japan
- [May 68](https://en.wikipedia.org/wiki/May_68), leftist French civil unrest
- [Diffie-Hellman key exchange](https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange)
- [Xi'an Incident](https://en.wikipedia.org/wiki/Xi%27an_Incident), where [Chiang Kai-shek](https://en.wikipedia.org/wiki/Chiang_Kai-shek) (leader of the Nationalists) is kidnapped by his generals [Yang Hucheng](https://en.wikipedia.org/wiki/Yang_Hucheng) and [Chang Hsueh-liang](https://en.wikipedia.org/wiki/Chang_Hsueh-liang), and forced to cooperate with the Communists against the invading Japanese
- [Abdullah Öcalan](https://en.wikipedia.org/wiki/Abdullah_%C3%96calan), imprisoned [PKK](https://en.wikipedia.org/wiki/Kurdistan_Workers%27_Party) leader
- [Oda Nobunaga](https://en.wikipedia.org/wiki/Oda_Nobunaga), Japanese warlord, who died because of the [Honnō-ji Incident](https://en.wikipedia.org/wiki/Honn%C5%8D-ji_Incident)
- [Ishiyama Hongan-ji](https://en.wikipedia.org/wiki/Ishiyama_Hongan-ji), former [Jōdo Shinshū](https://en.wikipedia.org/wiki/J%C5%8Ddo_Shinsh%C5%AB) temple/fortress, was burned down and replaced by [Osaka Castle](https://en.wikipedia.org/wiki/Osaka_Castle), and the reason why the city of [Osaka](https://en.wikipedia.org/wiki/Osaka) exists
- [Peninsular War](https://en.wikipedia.org/wiki/Peninsular_War), [Napoleon](https://en.wikipedia.org/wiki/Napoleon)'s invasion of Spain and Portugal
- [Thomas Cochrane, 10th Earl of Dundonald](https://en.wikipedia.org/wiki/Thomas_Cochrane,_10th_Earl_of_Dundonald), successful British Navy officer accused of stock exchange fraud, later participating in [Liberating Expedition of Peru](https://en.wikipedia.org/wiki/Liberating_Expedition_of_Peru) from the Spanish
- [Indonesia invades East Timor](https://en.wikipedia.org/wiki/Indonesian_invasion_of_East_Timor) to overthrow [Fretilin](https://en.wikipedia.org/wiki/Fretilin)
- [Special Region of Yogyakarta](https://en.wikipedia.org/wiki/Special_Region_of_Yogyakarta), a region of Indonesia *currently* hereditarily ruled by the [Yogyakarta Sultanate](https://en.wikipedia.org/wiki/Yogyakarta_Sultanate) and the [Duchy of Pakualaman](https://en.wikipedia.org/wiki/Pakualaman)
- [Nanboku-chō period](https://en.wikipedia.org/wiki/Nanboku-ch%C5%8D_period), when two opposing Japanese Imperial Courts existed, after the overthrow of the [Kamakura Shogunate](https://en.wikipedia.org/wiki/Kamakura_shogunate) and the failure of the [Kenmu Restoration](https://en.wikipedia.org/wiki/Kenmu_Restoration)
- [COINTELPRO](https://en.wikipedia.org/wiki/COINTELPRO), where the FBI unsurprisingly misbehaves
- [Transition to the New Order](https://en.wikipedia.org/wiki/Transition_to_the_New_Order), where [Suharto](https://en.wikipedia.org/wiki/Suharto) purges the [Indonesian Communist Party](https://en.wikipedia.org/wiki/Communist_Party_of_Indonesia), and overthrows [Sukarno](https://en.wikipedia.org/wiki/Sukarno)
- [Crypto Wars](https://en.wikipedia.org/wiki/Crypto_Wars), where the US Government tries to prevent the public and foreigners from using strong encryption
- [The Battle of Blair Mountain](https://en.wikipedia.org/wiki/Battle_of_Blair_Mountain), where striking coal members are bombed
- [Haymarket Affair](https://en.wikipedia.org/wiki/Haymarket_affair), where a bomb was thrown at police during a rally supporting the eight-hour work day
- [Tigray War](https://en.wikipedia.org/wiki/Tigray_War), a recent rebellion of the Tigrayan Government against the Ethiopian Government
- [Timur](https://en.wikipedia.org/wiki/Timur), conquerer and founder of the Timurid Empire, self proclaimed successor of Genghis Khan, and ancestor of the [Mughal Empire](https://en.wikipedia.org/wiki/Mughal_Empire)'s founders
- [Year of the Four Emperors](https://en.wikipedia.org/wiki/Year_of_the_Four_Emperors), a period of civil war in the Roman Empire
- [Frederick the Great](https://en.wikipedia.org/wiki/Frederick_the_Great), Prussian king, and military general
- [Tadeusz Kościuszko](https://en.wikipedia.org/wiki/Tadeusz_Ko%C5%9Bciuszko), leader of the Polish-Lithuanian [Kościuszko Uprising](https://en.wikipedia.org/wiki/Ko%C5%9Bciuszko_Uprising) against Russian rule, and American Revolutionary War hero
- [Favelas](https://en.wikipedia.org/wiki/Favela), Brazilian slums, some of which are ruled by cartels or vigilantes
- [Princely State](https://en.wikipedia.org/wiki/Princely_state), Indian prince ruled territory under the British
- [Annexation of Goa](https://en.wikipedia.org/wiki/Annexation_of_Goa), India invades Portugese ruled Goa
- [List of ethnic armed organisations in Myanmar](https://en.wikipedia.org/wiki/List_of_ethnic_armed_organisations_in_Myanmar)
- [Marcionism](https://en.wikipedia.org/wiki/Marcionism), early interpretation of Christianity
- [Lion-Eating Poet in the Stone Den](https://en.wikipedia.org/wiki/Lion-Eating_Poet_in_the_Stone_Den), shi shi shi shi...
- [Fuke-shū](https://en.wikipedia.org/wiki/Fuke-sh%C5%AB), [Shakuhachi](https://en.wikipedia.org/wiki/Shakuhachi) playing, basket wearing, Zen Buddhism sect
- [Zen Koans](https://en.wikipedia.org/wiki/Koan), "A monk asked Dongshan Shouchu, 'What is Buddha?' Dongshan said, 'Three pounds of flax.'"
- [Shugendō](https://en.wikipedia.org/wiki/Shugend%C5%8D), a religion combining Buddhism, folk religion, and Shinto mountain worship