A passphrase of random words and a password of random characters can both be strong. Entropy lets you compare them fairly.
What to build
Compare a random passphrase with a random password and say which is stronger.
Requirements
- Line 1: the word list size W and the number of words k. Line 2: the password length L and its pool size P.
- Passphrase bits = k × log₂(W); password bits = L × log₂(P).
- Print
passphrase X bits,password Y bits(one decimal each), thenpassphrase is stronger,password is stronger, orabout equalif they differ by less than 1 bit.
Input
Two lines.
Output
Three lines.
Constraints
2 ≤ W ≤ 1000000, 1 ≤ k ≤ 20, 1 ≤ L ≤ 64, 2 ≤ P ≤ 95.
Example
Input:
7776 4
8 95
Output:
passphrase 51.7 bits
password 52.6 bits
about equal
Why it works
Both choices are random, so each is measured by the same rule: the number of equally likely options, in bits.
Examples
Example 1 · Input
7776 4 8 95
Expected output
passphrase 51.7 bits password 52.6 bits about equal
Example 2 · Input
7776 6 10 62
Expected output
passphrase 77.5 bits password 59.5 bits passphrase is stronger
Hints
Hint 1
Compare the unrounded bit values, not the printed ones.
Solution
Show one correct solution (try it yourself first)
const [W, k] = prompt().trim().split(/\s+/).map(Number);
const [L, P] = prompt().trim().split(/\s+/).map(Number);
const phrase = k * Math.log2(W);
const word = L * Math.log2(P);
console.log(`passphrase ${phrase.toFixed(1)} bits`);
console.log(`password ${word.toFixed(1)} bits`);
console.log(Math.abs(phrase - word) < 1 ? "about equal" : phrase > word ? "passphrase is stronger" : "password is stronger");
▶ Open this solution in the compiler, then press Visualize to watch it run step by step