Posts

Showing posts with the label hackerrank

Exclusive Or

Find the XOR of two numbers and print it. Hint: Input the numbers as strings. Input Format First-line contains the first number X and the second line contains second number Y. The numbers will be given to you in binary form. Constraints 0 <= X <= 2^1000 0 <= Y <= 2^1000 Output Format Output one number in binary format, the XOR of two numbers. Sample Input 0 11011 10101 Sample Output 0 01110     CODE: a=input() b=input() c=""   if len(a)<=len(b):     k=len(b)-len(a)     l="0"*k     a=l+a else:     k=len(a)-len(b)     l="0"*k     b=l+b     for i in range(len(a)):     c+=str(int(a[i])^int(b[i]))   print(c)

Square in a Circle

  A square is kept inside a circle. It keeps expanding until all four of its vertices touch the circumference of the circle. Another smaller circle is kept inside the square now and it keeps expanding until its circumference touches all four sides of the square. The outer and the inner circle form a ring. Find the area of this ring. Input Input consists of multiple test cases. Each test case contains one integer denoting the side-length of the square between the two circles. Output Print the area of the ring. Sample Input 0 3 3 4 5 Sample Output 0 7.068583 12.566371 19.634954   Code: import math t=int(input()) while(t!=0):     t-=1     q=int(input())     a_o=q/math.sqrt(2)     a_i=q/2     print("%.6f"%(math.acos(-1)*(a_o*a_o - a_i*a_i)))    

Trouble with the Number System

Image
  Barua has developed his own Operating System known as "Barua OS" ( BOS ). One day while booting up his system he runs into a bug. You want to impress Barua so you jump in and offer to solve the bug yourself. Barua doesn't like binary numbers very much and his operating system uses a new number system called BNS ( Barua Number System ). The following are the properties of a number represented in BNS form : 1.       The number can only be made up of 2 distinct digits, one or zero. 2.       The number cannot start with zero. 3.       The number can have any number of zeroes, but only one instance of the digit one. For example 100, 1000, 10000 are Barua Numbers whereas 101, 502, 625 are not Barua Numbers. Unfortunately one decimal number has crept into a list of Barua Numbers and Barua OS cannot find its product. Can you? Input Format: First line contains an integer N, total number of elements in...

Residue Arithmetic

Image
  For operators with large integers, residue arithmetic is used. A residue of a number n modulo a prime p is defined as the remainder obtained when we divide a by p. For example, if the residue of 100 modulo 3 is 1. Two large primes are chosen and all numbers are expressed as modulo those primes. For example, if 7 and 3 were chosen as the primes, 25 would be represented as 4,1 in this representation, 3 would be represented as 3,3 and 30 would be represented as 2,0. Now if we do 25 * 3 - 30 in this model, we do the operation and compute the residue of the result with each of the primes in turn. SO with respect to prime 7, the result is 4 * 3 - 2 or 10 which has a residue of 3 modulo 7. With respect to 13, the operation is 12 * 3 - 4 = 32 whose residue is 6 modulo 13. Hence the result is 3,6 in the residue notation. There is only one number less than 91 which has this representation, and that is 45, which is what we expect. In this problem, we will be given 3 numbers a,b and c in res...

Consecutive Prime Sum

Image
Some prime numbers can be expressed as sums of other consecutive prime numbers. For example 5 = 2 + 3 17 = 2 + 3 + 5 + 7 41 = 2 + 3 + 5 + 7 + 11 + 13 Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to the constraint that summation should always start with number 2. Write code to find out a number of prime numbers that satisfy the above-mentioned property in a given range. Input Format: Each test case contains a number N <= 1000000000 Output Format: Print the total number of all such prime numbers which are less than or equal to N.   Code: import math def isprime(n):     if (n==2 or n==3):         return 1     if (n==1 or n%2==0 or n%3==0 ):         return 0     for i in range(5,int(math.sqrt(n)+1),6):         if(n%i==0 or n%(i+2)==0 ):             return ...