Vijay Parikh

Staying hungry. Staying foolish.

The Fifth Hyperfactorial


Close. But not exactly - one day isn’t exactly 86400000 milliseconds, it’s actually a few milliseconds more. To be percise its 8.64+7

John Carmack on Steve Jobs

An interesting post by Doom and Quake developer John Carmack.

My first impression of “Keynote Steve” was him berating the poor stage hands over “This Home Depot shit” that was rolling out the display stand with the new Mac, very much not to his satisfaction. His complaints had a valid point, and he improved the quality of the presentation by caring about details, but I wouldn’t have wanted to work for him in that capacity.

One time, my wife, then fiancée, and I were meeting with Steve at Apple, and he wanted me to do a keynote that happened to be scheduled on the same day as our wedding. With a big smile and full of charm, he suggested that we postpone it. We declined, but he kept pressing. Eventually my wife countered with a suggestion that if he really wanted “her” John so much, he should loan John Lassiter to her media company for a day of consulting. Steve went from full charm to ice cold really damn quick. I didn’t do that keynote.

Its always striking how much of a jerk Steve Jobs was. Sure he founded Apple and helped create all the gadgets we love, but is being a jerk really necessary trait for success?

Shortening URLs

How to design a system that takes big URLs like https://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n and converts them into a short 6 character URL. It is given that URLs are stored in database and every URL has an associated integer id.

One important thing to note is, the long URL should also be uniquely identifiable from short URLs. So we need a Bijective Function.

One Simple Solution could be Hashing. Use a hash function to convert long string to short string. In hashing, that may be collisions (2 long URLs map to same short URL) and we need a unique short URL for every long URL so that we can access long URL back.

A Better Solution is to use the integer id stored in database and convert the integer to character string that is at most 6 characters long. This problem can basically seen as a base conversion problem where we have a 10 digit input number and we want to convert it into a 6 character long string.

Below is one important observation about possible characters in URL.

A URL character can be one of the following:

  1. A lower case alphabet [‘a’ to ‘z’], total 26 characters
  2. An upper case alphabet [‘A’ to ‘Z’], total 26 characters
  3. A digit [‘0’ to ‘9’], total 10 characters

There are total 26 + 26 + 10 = 62 possible characters.

So the task is to convert a decimal number to base 62 number.

To get the original long URL, we need to get URL id in database. The id can be obtained using base 62 to decimal conversion.

Here is some code in ruby that implements a Base62 encoder and decoder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Base62
  CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars
  BASE = 62

  def self.encode(value)
    s = []
    while value >= BASE
      value, rem = value.divmod(BASE)
      s << CHARS[rem]
    end
    s << CHARS[value]
    s.reverse.join("")
  end

  def self.decode(str)
    str = str.split('').reverse
    total = 0
    str.each_with_index do |v,k|
      total += (CHARS.index(v) * (BASE ** k))
    end
    total
  end
end

And here is an example:

1
2
3
4
5
6
irb(main):001:0> n=12345
=> 12345
irb(main):002:0> shorturl = Base62::encode(n)
=> "3d7"
irb(main):003:0> t = Base62::decode(shorturl)
=> 12345

Stay tuned for a simple personal URL manager coming shortly…

GPS Distance

I recently had to calculate the distance between a large number of co-ordinates. I could have used one of the many Ruby Gems that are available but due to business limitations had to develop the code myself. After some research I came across the haversine formula.

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical triangles.

You can read the full details on the maths here. For those who just want the code here is the implemenation I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module GPS
  class Distance
    RAD_PER_DEG = Math::PI / 180
    GREAT_CIRCLE_RADIUS_MILES = 3956
    GREAT_CIRCLE_RADIUS_KILOMETERS = 6371 # some algorithms use 6367
    GREAT_CIRCLE_RADIUS_FEET = GREAT_CIRCLE_RADIUS_MILES * 5280
    GREAT_CIRCLE_RADIUS_METERS = GREAT_CIRCLE_RADIUS_KILOMETERS * 1000
    GREAT_CIRCLE_RADIUS_NAUTICAL_MILES = GREAT_CIRCLE_RADIUS_MILES / 1.15078

    attr_accessor :great_circle_distance
    attr_accessor :point1
    attr_accessor :point2

    def initialize(great_circle_distance = 0)
      @great_circle_distance = great_circle_distance
      @point1 = [0,0]
      @point2 = [0,0]
    end

    def to_miles
      calculate
      @great_circle_distance * GREAT_CIRCLE_RADIUS_MILES
    end
    alias_method :to_mi, :to_miles

    def to_kilometers
      calculate
      @great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS
    end
    alias_method :to_km, :to_kilometers

    def to_meters
      calculate
      @great_circle_distance * GREAT_CIRCLE_RADIUS_METERS
    end
    alias_method :to_m, :to_meters

    def to_feet
      calculate
      @great_circle_distance * GREAT_CIRCLE_RADIUS_FEET
    end
    alias_method :to_ft, :to_feet

    def to_nautical_miles
      calculate
      @great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES
    end
    alias_method :to_nm, :to_nautical_miles

    private

    # Radians per degree
    def rpd(num)
      num * RAD_PER_DEG
    end

    def calculate
      # Accept two arrays of points in addition to four coordinates
      if point1.is_a?(Array) && point2.is_a?(Array)
        lat2, lon2 = point2
        lat1, lon1 = point1
      elsif
        raise ArgumentError
      end
      dlon = lon2 - lon1
      dlat = lat2 - lat1
      a = (Math.sin(rpd(dlat)/2))**2 + Math.cos(rpd(lat1)) * Math.cos((rpd(lat2))) * (Math.sin(rpd(dlon)/2))**2
      @great_circle_distance = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))
    end
  end
end

Here is a quick script on how to use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
irb(main):001:0> d = GPS::Distance.new
=> #<GPS::Distance:0x007fb93b036050 @great_circle_distance=0, @point1=[0, 0], @point2=[0, 0]>
irb(main):002:0> d.point1 = [40.7457395,-73.991623]
=> [40.7457395, -73.991623]
irb(main):003:0> d.point2 = [40.9176771,-74.2082467]
=> [40.9176771, -74.2082467]
irb(main):004:0> d.to_meters
=> 26413.70207758391
irb(main):005:0> d.to_kilometers
=> 26.413702077583906
irb(main):006:0> d.to_miles
=> 16.40128793265138
irb(main):007:0> d.to_feet
=> 86598.80028439929
irb(main):008:0>

Hope this helps. If you have any questions, or comments feel free to leave a message or contact me directly.

Senators Propose Bug Bounties

Reminds me of this Dilbert cartoon I read years ago (accept I would code me a Tesla):

 - Dilbert by Scott Adams

From CNN:

U.S. senators want people to hack the Department of Homeland Security. On Thursday, Senators Maggie Hassan, a Democrat and Republican Rob Portman introduced the Hack DHS Act to establish a federal bug bounty program in the DHS... It would be modeled off the Department of Defense efforts, including Hack the Pentagon, the first program of its kind in the federal government. Launched a year ago, Hack the Pentagon paved the way for more recent bug bounty events including Hack the Army and Hack the Air Force...

The Hack the DHS Act establishes a framework for bug bounties, including establishing "mission-critical" systems that aren't allowed to be hacked, and making sure researchers who find bugs in DHS don't get prosecuted under the Computer Fraud and Abuse Act. "It's better to find vulnerabilities through someone you have engaged with and vetted," said Jeff Greene, the director of government affairs and policy at security firm Symantec. "In an era of constrained budgets, it's a cost-effective way of identifying vulnerabilities"... If passed, it would be among the first non-military bug bounty programs in the public sector.