Wednesday, February 13, 2019

Fastest way to check whether a given number is prime/Semi Prime


Below is my first discovery in the world of prime numbers:

 
Problem statement
A semi prime (also called biprime or 2-almost prime, or pq number) is a natural number that is the product of two (not necessarily distinct) prime numbers(Source:Wikipedia)
The only way to check whether a given number is prime with 100% probability is finding whether the given number has factors when you iterate between the value “2” and square root of the given number.
The same number of iterations are used to determine whether a given number is semi-prime.
Solution
With 100% probability we can check whether a given number is prime/semi-prime, with very very less time complexity.
Below is the process to follow:
1.       Find the cube root of the given number(let’s say this as “n”). Let’s say the cube root is “cn”
2.       Make a list of all the prime numbers till the prime number which is greater than “cn”, but is the least. So if “cn” is “4”, we need to take prime numbers till “5”.
3.       Check any of the prime number in step-2 divides “n”. If yes, then the “n” is neither prime nor semi-prime
4.       If we exhaust all the prime numbers in step-2 and still nothing divides “n”, then “n” is prime or semi-prime
Example
Let’s say we take any number below 10,000 and want to confirm whether it is prime/semi-prime/none.
Cuberoot of 10,000 is 21.544. Rounding off to nearest integer it is 22.
Take the prime numbers till 23(which is the least prime number that is greater than cuberoot of number we want to determine) and see if any of these divides our number. If nothing is able to divide then our number is prime/semi-prime.



In python the implementation will be as below, if we consider first 10,000 positive integers(note the array is initialized to first 9 prime numbers). Any of the first 10,000 positive integers can be evaluated to be prime/semi-prime using the below function. For numbers greater than 10,000 we just need to increment “arr” array.
Impact
Below are some of the possible applications for this solution:
1.       To check whether a given number is prime/not requires exponential time complexity as the number grows big. Although our solution results in Semi-primes along with primes, this helps in narrowing down quickly whether the number is not a prime.
2.       Semi-primes are used in RSA algorithm where the multiple of two large primes is used for modulus.
3.       Other applications that use large prime numbers in cyber security

Compared this with miller-rabin test with first 50,000 positive integers and the results generated are the same. Used 40 iterations for miller-rabin test which is minimum required to get good probability for Miller-rabin.
Although the results generated are same with Miller-rabin, the time taken to check whether the given number is prime/Semi-prime and print them with my solution is nearly half. Below are the times in a 1.6 GHZ clockspeed Ubuntu M/c.

Miller-Rabin(In seconds)
Proposed Solution(In Seconds)
Real
0.497
0.227
User
0.188
0.183
Sys
0.026
0.025

Friday, September 14, 2018

Installing tensorflow in windows easily

Before installing Tensorflow, first install python in your machine.

Use this link to download and install latest Python 3.6.2. This will automatically install Python, PIP and set environment variables. Don't mess with the default settings here.
https://www.python.org/ftp/python/3.6.2/python-3.6.2-amd64.exe

First install Microsoft Visual C++ Redistributable 2015 x64

Type CMD in windows search bar and right-click run as administrator.


Then you need to run the below command

C:\> pip3 install --upgrade tensorflow
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Now, tensor flow is installed in your computer. To verify the installation of tensorflow, in the same terminal run python and then below:

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

If there is no error and you see below output, then tensor flow installation is successful.

Hello, TensorFlow!

If you see error like AVX support not there, follow 

Uninstall existing tensorflow using "pip uninstall tensorflow", then reinstall it using "pip install <Path to downloaded WHL file>". Download this WHL file into your computer - github.com/fo40225/tensorflow-windows-wheel/blob/master/1.10.0/… , if you have a 3.6 Python and a 64-bit windows(ignore the amd you see). Otherwise navigate a step back in github and search for correct WHL. It works

Sunday, June 25, 2017

Understanding futures in stock market

Let us know talk about futures. Please complete going through the previous post on derivatives before reading this. What are futures? As you already know, futures are a kind of derivatives, whose value most closely resembles the price of the underlying stock.

For example, consider an RCOM stock. Futures have an expiry date, which is usually the last Thursday of the month. Today's date is 26th June, so we we have a 29th June derivative of RCOM which expires on 29 June and whose value is closely resembles the underlying stock. Below is a snapshot taken from the NSE website of 29th June future.


As you can see the closing price of the derivative on Friday at the market closing hours is Rs. 21.4. Now let's see the underlying stock RCOM closing price on the same day when the market closed which is as below.


As you can see the values are a close resemblance of each other. Also if you see their intraday chart which is a graph in the bottom right corner of both, you can observe both have almost same moment except the price point. This gives you a glimpse of future, and at any particular day if you can predict the movement of the stock you can buy its derivative and make good money even though you have around less than 20000 rupees in your account. Sometimes you can even get an almost equal amount of money as profit, that is the power of futures.

Now take a look at the derivative snap which I posted again by scrolling above. As you can see the derivative opened at 20.5 it reached an intraday high of 21.8 and intraday low of 19.9. This means as you can just see in the intraday chart, at different times of the day the derivative keep moving. Now, just consider you bought the derivative of RCOM when the derivative price is trading at 20.2. You know for sure that RCOM is trying to sell Assets and that is positive for the stock. So since the stock goes up, it means this future also will go up. So, as per  RCOM leverage rules, you can buy 12000 shares of RCOM future for about 16000 rupees. And it is mandatory that whatever you buy it has to be in multiple of 12000. Now you have invested your 16000 rupees in the morning by buying 12000 shares of rcom derivative at price rupees 20.2. Now wait till evening when the price reaches to do 21.2 and sell it off so you can see that you have a 1 Rupee profit per share. This can be automated, where you can put target at 21.2, and keep doing your other work also as all stock broking accounts now offer this feature. At around 2 PM your target is reached. Now, if you multiply 1 rupee profit per share with 12000 shares you got this 12000 rupees. So you can see the power of futures, in giving you the maximum value for your money invested at the same time even if you don't have the high amount of money to buy 12000 stocks. Isn't it great.

But there is a downside also. If the stock after you bought it for 20.2, would have gone to 19.2 you would have made a loss of 12000 rupees. So you must be cautious enough while buying the derivatives that you try to get it when you are buying for as low price as possible and when you are selling don't be greedy that it will reach more highs. Keep a target for your profits too. We will discuss about options in my next post.

Derivatives on NSE - Futures and Options

Today I am going to give you a brief about derivatives. So what are these derivatives, why are they called derivatives?

Derivatives can be termed as items which don't have any inherent value within themselves but which derive their value from stocks. These derivatives can be traded in stock market just like regular stocks but the difference lies in the amount of money you get when a derivative moves compared to the underlying stock. The amount of money you can make is really huge if you can just predict the underlying stock movement.


Hindalco Stock


For example consider the stock of Hindalco on NSE. The stock price is currently trading at rupees 190. Now if you buy July future of the stock it might be treating at about 192 rupees, but the value of this feature is entirely dependent on the underlying Hindalco stock. Now consider the underlying Hindalco stock moves from 190 to 192. The July feature of the stock move from 192 to 194. Now consider a person who has who has bought 3500 shares of Hindalco stock when the stock is the trading at Rs. 190 for which he will be spending 3500x190 rupees which is 665000 rupees. Now when the stock moves to 192 rupees, if he sells all the 3500 stocks he has, he will be gaining 3500x2 Rupees which comes around 7000 rupees profit. Now we usually don't have that kind of money, the usual retail traders like us. So what we can instead do is we can buy a future which is the currently trading at the 192 rupees, for which the broker charges you around 20000 rupees if you can assure that you will sell the stock that day itself, then when the stock moves to 194 you can sell the same stock and can get the same 7000 rupees profit but here your investment is only 20000 rupees so if you can just have 20000 rupees in your account you can get a profit of 7000 rupees just like that. That is advantage of derivatives.




Hindalco derivative(29th July Future)

The broker or the bank which you have trading account if you can show them that you will sell the derivative on the same day they will give you an option to buy the derivative stock using leverage(leverage simply means giving you option to buy the derivative as long as you hold 1:20 of the derivative value in your account). That leverage helps you get the derivative of Hindalco which would have cost more than 650000 at 20000 rupees only. What happens if you don't sell the derivative on the same day? so usually at the end of the day, just before the market closes whatever is the price of the derivative, the bank or the brokerage automatically squares off so that their leverage too is protected.

Prince William gets scolded by Queen Elizabeth II

Below is the video going viral of Queen taunting her son. But there is no reason why it is not royal. He immediately obeyed and this should send message across the world to respect your elderly.

What it takes for Donald Trump to be a game changer.

Less than 48 hours to go, for Indian prime minister Modi to meet Trump. Trump has been in office less than six months, but facing too much resistance within his own party along with Democrats. The Comey hearing, Mexico's veiled attacks, media against him using his old tweets and videos... all this have significant effect on a person's morale. Wonder how Trump deals with them, also his age doesn't give him succor.

May be Trump needs to borrow a leaf from Modi's text book on how to earn respect from your own party along with outsiders. If people are with you, automatically the political guys have to be on your side. Things that Modi did like making sure every citizen has a bank account, cooking gas for BPL families while taking away cooking gas subsidy from well off, demonitzation which is de-recognising high value notes from currency etc., Some are incremental, and some are revolutionary but citizens of a country are always behind a person trying to do something for them and which touches their lives. That's what Modi has done and Trump can also do.



Tweeting about stuff or talking is not enough, but Trump has to get to work sooner. He needs to work for middle classes, poor who needs jobs, give them proper healthcare that works, rebate in tuition fees for meritorious American students and a lot. He needs to push the bureaucracy to get things done. Need to take decisions fast enough and not just tweet or give interview about them. That's what Modi has done and Trump can follow.



Here T means Trump, and he should stop tweeting about whatever he knows immediately. A leader needs to maintain a oath of secrecy.

Thursday, June 22, 2017

Uber CEO Travis Kalanick resigns - Impact on Ola

As Travis resigns, many Indian Uber employees are in disarray. A Business standard report cites that everyday there is a meeting in Uber India office where employees are being assured of their job safety. Even though layoffs are not there, employees are quitting the company.

The capital dumping by Uber has a large impact on Indian ride hailing service industry that except Ola, all other companies like Taxi For Sure have shut shop or merged themselves with bigger entities. Now it is only Ola vs Uber in India. If investors get jumpy and start taking decisions about not dumping capital, there will be a rational playing field between Ola and Uber. This way, the more innovative guy wins, but not the one with more money.

Ola has a chance now, as it has clear leadership to keep fighting. And both Ola and Uber can co-exist in a big market like India. Let us wait and watch

Image Courtesy: Hans India

Wednesday, June 21, 2017

Opening a new account with rksv/upstox

Today I will give you the process to open a new account with upstox also called as rksv earlier without sending any forms by post by sitting at the comfort of your home. As you might be already knowing, upstox is purely for stock market transactions(discount broker they call) and it charges very low(dead low) compared to your banks.



You can simply goto https://new.upstox.com and hit signup to follow the process. But before you do that, you need to have below things mandatory:
  1. A cheque book
  2. PAN card
  3. Last filed ITR form/Last three months payslip/Bank Statement
  4. Aadhaar card linked with the mobile number you are giving
The cheque book should contain your name and should match with the bank account details you give. Make sure you have internet banking account also for the bank account details you are giving. This is because you can only add money to your upstox account(more about add money latter) or withdraw money from your upstox account into that one bank account only. It is impossible to do it from another bank account.

Now follow the process in the website. At the end of the process it asks you to upload video of you holding pan card close to your face. Some times it doesn't work. But don't worry, just hit submit. They will latter share one whatsapp number with you to share the video, if you didn't attach the video before. Any doubts, please comment below, will answer within 24 hours.

Note: Adding money to your upstox account means, transferring money from your bank account to upstox account. You cannot directly use your bank money to trade in stock market.

How Indian Presidential Elections work

This is how Indian an Indian President is elected. You will come to know the importance of an MP and MLA in presidential election. Must for people who are preparing for central government examinations within India


Yoga Sutras available on amazon prime

June-21st is international Yoga day and if you want to learn Yoga, from the comfort of your home there is a way. Make sure you are subscriber of Amazon prime, and search for Yoga Sutras. There are about 13 videos and each video is 18 minutes. Directly move to the 11th minute of video to practice Yoga in each of the videos. The first 10 minutes they just show different Yogic postures. Very easy to understand, but you need to give your body some time and don't rush to do exactly what they do. Keep it slow.


Kumble vs Kohli - Reactions sent to Republic TV

Arnab's Republic TV received self recorded videos of what Indians think, on the Kumble's resignation and the letter he posted on twitter. Below is the compilation of videos.


Monday, December 15, 2014

Lumia Gestures

Microsoft has released Gestures beta app, which is soon to be inbuilt in future windows versions. A simple glance to it's call features is below.


Tuesday, December 9, 2014

Easy authentication - NokNok

NokNok is a product software company, that specializes in authentication hardware/software. The below video speaks about their recent plans and updates. Slowly, we can forget things like forgot password etc., with biometric authentications, that use voice/keys/fingerprint and the list is going on.



Bitcoin

Lot has been in news about bitcoins. Tidbits about it.

Bitcoin has been created by Satoshi Nakomoto in 2009 and released as open source software. There are a finite set of coins, which can be mined(by solving complex algorithmic problems) or can be bought from people who have already mined. They are used for payments in e-commerce etc., similar to normal currency. Bitcoin is not a Fiat currency, since no recognised central government of any country backs it. It is hailed by it's followers as first unified currency for all the world.



But don't think you can mine a bitcoin. It's not simple today, since as the coins grew scarce, the complexity of problems goes up. Today people are renting huge offices and running multiple computers to mine them. So it's near impossible to mine. The easy way would be to buy.

The speculation about their rates, in the initial days was high, and there is a saying that a man bought an apartment in London selling the bitcoins he mined. But today, their price is not volatile and it has become a bit flat. So no use in even buying them. Just for the sake of having an antique piece, the first sign of liberty in currency, you can have it.