Friday, 30 September 2016

Discussion on problem 1 from project euler

Problem #1

Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Problem Break Down

Natural numbers: In mathematics, the natural numbers are those used for counting and ordering. In common language, words used for counting are “cardinal numbers” and words used for ordering are “ordinal numbers”. Wiki

Note: people are ambiguous about natural numbers too. They confuse between whole numbers and natural numbers and rightly so.


Divisibility by 3: sum the digits of the number and if that sum is divisible by 3, that number is divisible by three.

number (A) sum of digits (B) (B) divisible by 3 (C) (A) divisible by 3 (D)
1 1 no 1 is not divisible by 3
2 2 no 2 is not divisible by 3
3 3 yes divisible
4 4 no 4 is not divisible by 3
5 5 no 5 is not divisible by 3
6 6 no divisible
7 7 no 7 is not divisible by 3
8 8 no 8 is not divisible by 3
9 9 no divisible
10 1 no 10 is not divisible by 3
11 2 no 11 is not divisible by 3
12 3 no divisible
13 4 no 13 is not divisible by 3
14 5 no 14 is not divisible by 3
15 6 no divisible

Note that divisibility here means whole division and not fractional result.

Upto 15
numbers that are divisible by 3: 3, 6, 9, 12, 15
sum = 3 + 6 + 9 + 12 + 15 = 45

Divisibility by 5: If least significant digit is either 0 or 5, it is divisible by 5.

number (A) Least Significant Digit (B) (B) is 0 or 5 (C) (A) divisible by 5 (D)
1 1 no 1 is not divisible by 5
2 2 no 2 is not divisible by 5
3 3 no 3 is not divisible by 5
4 4 no 4 is not divisible by 5
5 5 yes divisible
6 6 no 6 is not divisible by 5
7 7 no 7 is not divisible by 5
8 8 no 8 is not divisible by 5
9 9 no 9 is not divisible by 5
10 0 yes divisible
11 1 no 11 is not divisible by 5
12 2 no 12 is not divisible by 5
13 3 no 13 is not divisible by 5
14 4 no 14 is not divisible by 5
15 5 yes divisible

Note that divisibility here means whole division and not fractional result.

Upto 15
numbers that are divisible by 5: 5, 10, 15
sum = 5 + 10 + 15 = 30

Sum of numbers that are divisible by 3 or divisible by 5

45 + 30 = 75


But what about numbers like 15, 30 and so on…

Well, 15 is divisible by 3 and 5. If we are counting the numbers that are divisible by 3 or 5; we have counted 15 twice above.

We need to take out the numbers that are divisible by both.

Upto 15
Numbers that are divisible by 3 and 5 = 15
Sum of numbers divisible by 3 = 45
Sum of numbers divisible by 5 = 30
Sum of numbers divisible by 3 or 5 = 45 + 30 - 15 = 60

Let’s check:

3 + 5 + 6 + 9 + 10 + 12 + 15 = 60


Code

pseudo

Getting the numbers that are divisible
by a given number N
below `upto`

numbersDivisibleBy ( N, upto ) {
    numberArray = [];
    loop i in 1 to upto:
        if i modulus N is 0:
            numberArray.add(N)
    return numberArray



divisibleBy3 = numbersDivisibleBy(3, 15)
divisibleBy5 = numbersDivisibleBy(5, 15)
divisibleBy15 = numbersDivisibleBy(15, 15)

result = (diff (sum (divisibleBy3, divisibleBy5), divisibleBy15)

javascript (in chrome console)

We are using arrow syntax (input => output).

const divisibleBy = (n, upto) => {
    let numbers = [];
    for(let i = 1; i <= upto; i++ ) {
        if(i % n === 0) {
            numbers.push(i);
        }
    }
    return numbers;
}

let divisibleBy3 = divisibleBy(3, 9);

let divisibleBy5 = divisibleBy(5, 9);

let divisibleBy15 = divisibleBy(15, 9)

divisibleBy3
[3, 6, 9]
divisibleBy5
[5]
divisibleBy15
[]

Our result can be calculated as:

divisibleBy3.reduce((p, c) => p+c, 0) + divisibleBy5.reduce((p, c) => p+c, 0) - divisibleBy15.reduce((p, c) => p+c, 0) 

Note that , 0 is defensive in first two instances and a necessity in last one and we are using Array.reduce

Questions still left

What if we need to find sum of numbers divisible by 3 upto Number.MAX_SAFE_INTEGER (=== 9007199254740991 in javascript)?

let divisibleBy3 = divisibleBy(3, Number.MAX_SAFE_INTEGER)
// oops, crash

enter image description here

What if we need to sum all such numbers? How big will the sum become?

What crashed dev tools? A fact that Arrays can keep only limited numbers in them?

and more.

Status report: Where I am and Project Euler

Status Report > Where I am and Project Euler

Yesterday a colleague of mine had a blog opened on his laptop; I asked what was that. He told me that it was a blog that he used to write. We shared links and ended up taking tour of things like ProjectEuler, HackerRank, briiliant.org, my old blogs, norvig.com etc. It was fun filled Friday evening.

So I got thinking about me being out of the learning loop. I am actually learning a lot, but not mathematics or programming. I am not coding that much. These days (circa 2016) I am much interested in finance and human behavior than ever. In technology, all I am interested is EcmaScript and NodeJS.

I am also trying to learn basic mathematics that I skipped over in college; integration, derivation, statistics etc.

I love solving problems but haven’t been solving problems for my fun these days. I think I should start learning and solving problems again. But here lies the rub: I want to learn a lot of stuff; enough that I won’t have time to …

You know what, no more excuses. I love solving problems and I’d be going through Project Euler problems once again from the beginning.

enter image description here

I’d be solving, writing code and posting for each of these problems.

Summary

I need to solve problems again. I need to start blogging again. I am going to do both.

Friday, 16 September 2016

ACID properties

For a transaction ACID is very important. It stands for Atomicity, Consistency, Isolation and Durability of the Transaction.

Atomicity

A transaction can be either be waiting to executed or would have executed. There is no middle ground/executing for a transaction.

Consistency

When a transaction is finished, system will be in the consistent state.

Isolation

A transaction doesn’t know about other transactions or their side-effect.

Durability

Once a transaction has succeeded, it would be part of the system even if system fails.

Let’s take money transfer as the example. Suppose, you moved X from account A to account B. Here are the tasks in the transaction.

  1. Take money out of account A.
  2. Put money in account B.
  3. Make sure that account balance of A is now less than it was earlier by X.
  4. Make sure that account balance of B is now more than it was earlier by X.
  5. Business rule checks: balance validity etc.

If any of the steps fail, transaction will be rolled-back; ensuring atomicity, consistency.

Take this parallel transaction in to the account: Move Y money from account A to account B; in such a way that X + Y is more than what account A has.

  1. Take money out of account A.
  2. Put money in account B.
  3. Make sure that account balance of A is now less than it was earlier by Y.
  4. Make sure that account balance of B is now more than it was earlier by Y.
  5. Business rule checks: balance validity etc.

Now, one of the transaction (x, y) will fail because account A doesn’t have enough balance. If they executed at the same time, both the transactions will see, account balance Z in account A. If they end at the same time, both should succeed; which will violate business rules. But it can happen. To prevent from this issue, Isolation is required.

Isolation can mark the account A unreadable and unwritable until one transaction is finished.

If machine looses power while executing the transaction, it might leave the application in the inconsistent state. This can be prevented if transaction logs everything. Even if the system crashes, logs will let transaction system know what to rollback or how many transactions are done. This is Durability.


Thing to note: SOAP is ACID compliant; REST is not; and ACID over web/http doesn’t make sense.

Use REST where you can. If you really need, use SOAP.

Monday, 29 August 2016

Clash Of Clans update

There's nothing left to upgrade, so Town Hall is on upgrade and in 2 hours it'll be TH3.

Sunday, 28 August 2016

TownHall Mayor - idea for new troop

Town Hall Mayor

A so fat guy that you haven't seen in ages. Living in TH from L1.

It protects village.
Very slow moving.

As soon as the village destruction is 40%, it wakes up. Mayor attracts first six types of  troops i.e. from barbarian to balloon.

Powers

High Hit Points, say 3500+
Attracts attacking forces.
On upgrade, becomes leaner and increases speed.

Bad investment calls and how to avoid them via Clash of Clans

Well, we ran it into the ground.


At present I am stuck here.

Stuck!
1. By my own rules
2. Bad investment calls
3. Experience helped me with #2
4. Executing mindlessly

As I discussed that I am going to follow Builder queue to upgrade my base/village; I am stuck now.

Here are the reasons that I think got me here:

1. By my own rules


If you read my last post you will be able to read that I have created some rules for playing Clash of Clans as challenge to myself.

I have got those rules out in a post of their own: Challenging rules for playing Clash of Clans.

As per rules, I have to follow next thing in the queue. But, I don't have resources left.

2. Bad investment calls


I did some village cleaning with precious elixir and gold. That was a bad call. I lost elixir and now cannot make enough of it.

3. Experience sucks


I made that bad investment call because that's what I did on my TH6 and TH8 villages. As soon as a builder freed up and I had elixir (which I have tons of on those bases), I'd cleanup village.

4. Executing mindlessly


I didn't take note of the situation with whole heart (actually mind) and let my experience guide me. Made some bad calls and have rigid rules. Flexibility in personal, professional, religious, diplomacy, politics, gaming etc. rules is required.

Due to the findings, I am updating rule #1, I am going to change it to: I will pick one of the task from the builder's queue per builder.

How to avoid bad investment calls?

1. Flexibility


Give yourself and your team a little bit of flexibility. Rigidity hurts.

2. Know temporal impact of your calls today.


Had I just spent 30 seconds thinking about what will the impact of cleaning action be, I would have saved my self from this headache.

Always think if future you is going to be happy by this decision that you're making now.

3. Experience is overrated


If you have experience in something, all it means is that you have done something a particular way. It does not guarantee that you have learned something from it. It doesn't guarantee that you will be successful next time. It doesn't guarantee that you'll get better results next time. What it does guarantee however is that you'll think (to some degree) that it's the way to achieve success.

4. Execution should be mindful


While operating anything; heavy damage machinery or finances, be mindful of your actions.

Thanks.

Challenging rules for playing Clash of Clans

Rules to abide by


1. I will upgrade as per the Builder queue suggestion. Pick one of the five suggestions as per the condition demands.
2. I will not buy from shop until on-map items are upgraded fully.
3. I will not upgrade TH until on-map items are upgraded to max and until there are things in shops.
4. I will not attack needlessly. I will attack only when a builder is free or is about to be free.
5. I will try to sync my builders.

Update #1

1. I will upgrade as per the Builder queue suggestion. 

To

1. I will upgrade as per the Builder queue suggestion. Pick one of the five suggestions as per the condition demands.