Word Frequency

Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume: words.txt contains only lowercase characters and space ’ ’ characters. Each word must consist of lowercase characters only. Words are separated by one or more whitespace characters. Example: Assume that words.txt has the following content: the day is sunny the the the sunny is is Your script should output the following, sorted by descending frequency: ...

October 27, 2021 · 1 min · oschvr

10001st prime

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? At first glance, a simple iteration whilst checking if index position is prime, would suffice. A pseudocode attempt: sum = 0 for i = 2; i <= ?; i++ { if(isPrime(i)){ sum++ if ( sum >= 10001) { return i } } } Using the solution in go to check if is prime from former examples: ...

May 12, 2019 · 3 min · oschvr

Sum square difference

The sum of the squares of the first ten natural numbers is, $$ 1^x1D2 + 2^2 + … + 10^2 = 385 $$ The square of the sum of the first ten natural numbers is, $$ (1 + 2 + … + 10)^2 = 552 = 3025 $$ Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. ...

May 9, 2019 · 2 min · oschvr

Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? At first glance, and as a concept refresher, we’ll have to use **GCD* and **LCM**. GCD stands for Greatest Common Divisor. GCD is the largest number that divides the given numbers. LCM stands for Lowest Common Multiple. LCM is the smallest number that is multiple of a and b. To find the LCM, we can use the following formula: ...

May 7, 2019 · 2 min · oschvr

Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers The first observation is that the number should be in the range of 10000 and 998001. There’s a couple aproaches to the problem, such as: Multiplicating two 3-digit numbers and checking the result if its a palindrome Creating palindromes and getting the factors to see if that pair is what we’re looking for. I’ll go for the first approach. First I’ll create an int reversal function to call whenever I want to check if a number is a palindrome. ...

May 3, 2019 · 3 min · oschvr