[ create a new paste ] login | about

mohit_at_codepad

Name: Mohit Jain
Email:
Site/Blog: http://www.youtube.com/user/mohitjain1983/featured
Location: Noida
Default language: C++
Favorite languages: C++, Haskell, Java
About: Proffessional with extensive experience in C++ and good command over C language. Experience to develop postscript interpreter, and working for a document company. (printer, scanner, finisher, document image processing, rf reader/writer, developing parser/writer for document formats like pdf/xls/ppt/doc/docx and printer job formats like PCL-XL, PS etc). Google code jam and ProjectEuler handle is mohitj and codechef and top coder handle is mjbpl. (My old topcoder handle was mj_india but I deleted it and now compete with name mjbpl) I speak English and Hindi natively, Japanese fluently and a few basic words of Mandarin. Recently I spend my after office hours in studying discrete mathematics.

Saved pastes by mohit_at_codepad:

C++, pasted on Dec 24:
1
2
3
4
5
/* Find log base 2 */
/* Copied as it is from : http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
int m1(unsigned int v)
{
  const bool isLittleEndian = true;
...
view (38 lines, 1638 lines of output)
C++, pasted on Sep 4:
1
2
3
4
/* This program emulates population count for GPUs */
/* Memory look up is very costly and so is the integer arithmetic */
/* Supports parallel processing and there are plenty of optimizations */
/* possible in this algo making it suitable for the requirement */
...
view (37 lines)
C, pasted on Sep 2:
1
2
3
4
/* Fast method to convert 565 to UNORM 8 */
/* This would retain the perfect black and white by */
/* mapping all 0 to all 0 and all 1 to all 1 and almost */
/* linear mapping for the remaining */
...
view (51 lines, 115 lines of output)
C++, pasted on May 12:
1
2
3
4
5
// Q: Find minimum and maximum number made out of all 9 digits which is a perfect square


bool isPerfectSquare(const char *s) {
  int num;
...
view (54 lines, 2 lines of output)
C++, pasted on Apr 16:
1
2
3
4
// Print the sides of right angled triangle whose sides are whole numbers and the perimeter is 1000.
// Whole numbers are analogous to unsigned integers in C.
// 12 is a whole number, but 12.5 is NOT a whole number.
// What are the optimizations possible?
...
view (37 lines, 1 line of output)
C, pasted on Apr 3:
1
2
3
4
5
/* Problem statement: Write a function to perform integer division */
/* These numbers are signed integer and are ALWAYS positive */
/* Try to minimize the branch and number of operations */
#include <stdio.h>
#include <stdlib.h>
...
view (81 lines, 1 line of output)
C, pasted on Feb 19:
1
2
3
4
5
struct abc {
  char ch[15];
  short int si[12];
  int def;
  long int li[10];
...
view (20 lines, 2 lines of output)
C++, pasted on Jan 30:
1
2
3
4
5
char sample1[] = "MohitJain";
char sample2[] = "India Asia World Earth";
char sample3[] = "ttt";
char sample4[] = "iii";
char sample5[] = "dead";
...
view (51 lines, 9 lines of output)
C++, pasted on Jan 24:
1
2
3
4
int my_rand(void)
{
  return rand() % 371;
}
...
view (52 lines, 1 line of output)
Haskell, pasted on Oct 21:
1
2
3
4
-- http://nabetani.sakura.ne.jp/hena/ord5railsontiles/
-- Input is 9 tiles
-- Arrange tiles and enter from Tile B, top
-- Trace the rail and print the tiles traversed until you corss 3 X 3 boundary
...
view (68 lines, 21 lines of output)
C++, pasted on Oct 20:
1
2
3
4
// http://nabetani.sakura.ne.jp/hena/ord5railsontiles/
// Input is 9 tiles
// Arrange tiles and enter from Tile B, top
// Trace the rail and print the tiles traversed until you corss 3 X 3 boundary
...
view (135 lines, 21 lines of output)
C++, pasted on Jul 26:
1
2
3
4
5
/// Problem statement: There are n doors and n students.
/// Initially all doors are closed.
/// Then each student goes and perform the following
/// kth student toggle door number k, 2k, 3k .... etc
/// For ex: Student 1 toggles all door (If it is open, close it and vice-versa)
...
view (23 lines, 1 line of output, 4 comments)
C++, pasted on Jul 20:
1
2
3
4
5
// Print the sides of right angled triangle whose sides are whole numbers and the perimeter is 1000.
// Whole numbers are analogous to unsigned integers in C.
// 12 is a whole number, but 12.5 is NOT a whole number.
// What are the optimizations possible?
#include <cassert>
...
view (53 lines, 2 lines of output, 1 comment)
C, pasted on Jul 20:
1
2
3
4
5
/* Find minimum distance between a point and a line _segment_       */
/* 1. Vector calculation (drop a perpendicular) method              */
/* 2. Ternary search (Enough binary search :) let's move to ternary */
#include <stdio.h>
#include <math.h>
...
view (109 lines, 12 lines of output)
C++, pasted on Jul 20:
1
2
3
4
5
/**
 * There is a crazy king.
 * He has a rule that a shopkeeper must label distinct price to every item in his shop.
 * i.e. No two items can have the same price.
 * Moreover, he has certain rules about the maximum price an object may have.
...
view (189 lines, 18 lines of output)
C++, pasted on Jul 18:
1
2
3
4
5
/// Problem statement: Fill a W X H matrix in spiral fashion outside to inside clockwise
/// For ex: 4 X 6 matrix would look like
///  1       2       3       4
/// 16      17      18       5
/// 15      24      19       6
...
view (57 lines, 4 lines of output)
C++, pasted on Jul 18:
1
2
3
4
5
/// Problem statement: Fill a W X H matrix in spiral fashion outside to inside clockwise
/// For ex: 4 X 6 matrix would look like
///  1       2       3       4
/// 16      17      18       5
/// 15      24      19       6
...
view (58 lines, 4 lines of output)
C++, pasted on Jul 17:
1
2
3
4
5
// Problem statement: Find the minimum number of tests required to
// determine the lowest floor in a building from which when we drop
// an egg it should _NOT_ break.
//
// - An egg that survives a fall can be used again.
...
view (64 lines, 15 lines of output)
C, pasted on Jul 16:
1
2
3
4
5
/* Find minimum distance between a point and a line _segment_       */
/* 1. Vector calculation (drop a perpendicular) method              */
/* 2. Ternary search (Enough binary search :) let's move to ternary */
#include <stdio.h>
#include <math.h>
...
view (109 lines, 12 lines of output, 4 comments)
C, pasted on Jul 16:
1
2
3
4
5
/**
 * An absent minded professor designed a house of 27 floors
 * but forgot to build any staircaes.
 * Now he has 4 very light weight ladders of length 1, 4, 13 and 14 floors.
 * To go to any floor he use any ladder to climb up and bring other
...
view (34 lines, 4 lines of output, 1 comment)
C++, pasted on Jul 11:
1
2
3
4
5
// Populate prime numbers using wheel factorization
// Algorithm should be fast enough to print 10001st prime number in less than 2 second
// (Before encountering timeout on codepad)
// 10001st prime number is 104743
// Is it possible to write a code that can print 9,99,999th prime number?
...
view (54 lines, 10639 lines of output)
Haskell, pasted on Jul 9:
1
2
3
4
5
-- Original problem statement @ http://nabetani.sakura.ne.jp/hena/1/
-- Input is game steps for tic tac toe
-- o takes the first move. Moves keep alternating between o and x then onwards
-- If o or x makes a line of 3 in any direction, it is declared as won
-- It o or x makes a move over already marked block, it is declared as foul
...
view (68 lines, 25 lines of output)
C++, pasted on Jul 9:
1
2
3
4
5
// Original problem statement @ http://nabetani.sakura.ne.jp/hena/1/
// Input is game steps for tic tac toe
// o takes the first move. Moves keep alternating between o and x then onwards
// If o or x makes a line of 3 in any direction, it is declared as won
// It o or x makes a move over already marked block, it is declared as foul
...
view (248 lines, 104 lines of output)
C, pasted on Jul 3:
1
2
3
4
5
// Implement your own atoi
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
...
view (48 lines, 21 lines of output)
C, pasted on Jun 28:
1
2
3
4
5
// Calculate xor of all numbers from 1 to N
// Testing for numbers upto 150000
// Same concept works well for large numbers also

#include <stdio.h>
...
view (50 lines, 10 lines of output)
C++, pasted on Jun 20:
1
2
3
4
5
/// Write a function to find the missing number in a list
/// @param[in] arr Input array
/// @param[in] size Size of array
/// All elements in array are unique and in range 1 to size + 1
/// @ret Missing number
...
view (37 lines, 10 lines of output)
C, pasted on Jun 18:
1
2
3
4
5
// Simple line drawing function
#include <stdio.h>
#include <math.h>

#define WIDTH  1000
...
view (43 lines, 42 lines of output)
C++, pasted on Jun 17:
1
2
3
4
// Find the closest matching number from a binary search tree
#include <iostream>
#include <cassert>
using namespace std;
...
view (113 lines, 7 lines of output)
C, pasted on Jun 17:
1
2
3
4
5
#include <stdio.h>
#include <string.h>
#define MAX_LEN_OF_STRING 500

void reverse_(char *b, char *e) {
...
view (70 lines, 3 lines of output)
C, pasted on Jun 16:
1
2
3
4
5
#include <stdio.h>
#include <ctype.h>
#include <limits.h>

/// @brief Return first repeating character
...
view (43 lines, 27 lines of output)
C, pasted on Jun 15:
1
2
3
4
5
// Increment a very large unsigned number by one
#include <stdio.h>
#include <string.h>

void incrementUnsignedIntByOneAndPrint(const char *s) {
...
view (47 lines, 39 lines of output, 1 comment)
C++, pasted on Jun 14:
1
2
3
4
5
#include <string>
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
...
view (40 lines, 3 lines of output)
C, pasted on Jun 13:
1
2
3
4
5
// Calculate the value of pi using Gauss-Legendre method
// http://en.wikipedia.org/wiki/Gauss%E2%80%93Legendre_algorithm
#include <stdio.h>
#include <float.h>
#include <math.h>
...
view (25 lines, 1 line of output)
C, pasted on Jun 12:
1
2
3
4
5
#include <stdio.h>
#include <math.h>
#include <assert.h>

#define MAX_N 50000
...
view (53 lines, 5133 lines of output)
C, pasted on Jun 12:
1
2
3
4
// Reverse all vowels in a string
#include <stdio.h>
#include <string.h>
#include <ctype.h>
...
view (40 lines, 1 line of output)
C, pasted on Jun 11:
1
2
3
4
5
// find fraction that support fallacy proof (howlers)
#include <stdio.h>
//#define const

void printFallacyDecends(int n, int d) {
...
view (23 lines, 4 lines of output)
C, pasted on Jun 11:
1
2
3
4
5
// find fraction that support fallacy proof (howlers)
#include <stdio.h>
//#define const

void printFallacyDecends(int n, int d) {
...
view (19 lines, 4 lines of output, 1 comment)
C++, pasted on Jun 9:
1
2
3
4
5
// And equation solver
// For more details please check Topcoder SRM#545 Div II easy (250) question
#include <iostream>
#include <algorithm>
#include <numeric>
...
view (66 lines, 7 lines of output)
C++, pasted on Jun 9:
1
2
3
4
5
// Find all changed number in rank array containing roll numbers
// Input an array containing n elements
// These elements are numbered 1 to n
// Some elements have changed (not 1 to n), find them
// Function prototype: void printMissing(int *arr, int n);
...
view (57 lines, 6 lines of output, 1 comment)
C++, pasted on Jun 6:
1
2
3
4
5
/// Calculate F(N+1) from F(N) where
///   F(N) is Nth Fibonacci number
///   N > 10
#include <iostream>
#include <iomanip>
...
view (62 lines, 91 lines of output, 1 comment)
C++, pasted on Jun 6:
1
2
3
4
5
/// Find all primes between N and N+k
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
...
view (342 lines, 20 lines of output, 1 comment)
C++, pasted on Jun 4:
1
2
3
4
5
/// Find median of 5 numbers
/// Result:
/// Function  Algorithm       Swap(min, max): average Comparision(min, max): average
/// Algo1     Stl nth_element Swap (0, 3): 1.566      Comp (11, 23): 14.5832
/// Algo2     Heap            Swap (0, 5): 2.9146     Comp (4, 8): 6.7076
...
view (227 lines, 2 lines of output)
C++, pasted on Jun 1:
1
2
3
4
5
/// Find pythagorus triplet in an array
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
...
view (43 lines, 28 lines of output)
C++, pasted on Jun 1:
1
2
3
4
5
// Find square root of a function using binary search.
// int b_sqrt(int num);
// returns -1 if num is out of range (0 to 1,000,000)
// Returns integer part of square root otherwise.
#include <iostream>
...
view (44 lines, 1 line of output)
C++, pasted on Jun 1:
1
2
3
4
// Compare babylonian and newton raphson
#include <stdio.h>

int iter = 0;
...
view (48 lines, 2 lines of output, 1 comment)
C++, pasted on May 31:
1
2
3
4
5
/// Write a function to find the missing number in a list
/// @param[in] arr Input array
/// @param[in] size Size of array
/// All elements in array are unique and in range 1 to size + 1
/// @ret Missing number
...
view (37 lines, 1 line of output, 1 comment)
C++, pasted on May 24:
1
2
3
4
5
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <algorithm>
#include <sys/time.h>
...
view (387 lines, 184 lines of output, 1 comment)
C++, pasted on May 14:
1
2
3
4
5
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;
...
view (49 lines, 3 lines of output)
C, pasted on May 1:
1
2
3
4
5
/**
 * Convert a roman numeral into equivalent decimal number
 * If the number is invalid numeral or out of range (1-1000)
 * return 0
 * Also check: http://codepad.org/2LG4SbjJ
...
view (196 lines, 13 lines of output, 3 comments)
C++, pasted on Apr 27:
1
2
3
4
5
// Big numbers
// Author: Mohit Jain
#include <string>
#include <sstream>
#include <iostream>
...
view (149 lines, 1 line of output)
C, pasted on Apr 26:
1
2
3
4
5
#include <stdio.h>
#include <math.h>

int main() {
  int i = 0;
...
view (17 lines, 1 line of output)
C++, pasted on Apr 26:
1
2
3
4
5
#include <stdio.h>
#include <math.h>

int main() {
  int i = 0;
...
view (12 lines, 1 line of output)
Haskell, pasted on Apr 26:
1
2
3
4
5
-- Count number of digits in 100!
-- I think the answer is below 192 but above 92
-- One can use sterling's formula to estimate the number of digits

-- Expected output is 158
...
view (16 lines, 1 line of output)
C, pasted on Apr 26:
1
2
3
4
/* Print the value of pi */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
...
view (24 lines, 1 line of output)
C, pasted on Apr 26:
1
2
3
4
/* Print the value of pi */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
...
view (21 lines, 1 line of output)
C, pasted on Apr 26:
1
2
3
4
5
/* Print the value of pi */
#include <stdio.h>
#include <stdlib.h>
#if 0
#define const
...
view (24 lines, 1 line of output)
C, pasted on Apr 25:
1
2
3
4
// Print the value of pi
#include <stdio.h>
#include <stdlib.h>
//#define const
...
view (24 lines, 1 line of output, 1 comment)
C, pasted on Apr 25:
1
2
3
4
// Print the value of pi
#include <stdio.h>
#include <stdlib.h>
//#define inline
...
view (27 lines, 1 line of output, 2 comments)
C++, pasted on Apr 11:
1
2
3
4
5
// Convert a roman numeral into equivalent decimal number
// If the number is invalid numeral or out of range (1-50)
// return 0

#include <string>
...
view (57 lines, 7 lines of output)
C, pasted on Apr 4:
1
2
3
4
5
/*
 * Problem statement
 * Input file is a text file containing multiple test cases.
 * First line contains two numbers count of rows
 * and number of characters in each row.
...
view (270 lines, 3 lines of output)
Haskell, pasted on Apr 2:
1
2
3
4
5
-- Written by Mohit Jain
-- Input is a poker hand
-- Output the analysis as described @ snippet below
-- http://codepad.org/HS8z6MZd
module Main where
...
view (26 lines, 42 lines of output, 1 comment)
Haskell, pasted on Mar 27:
1
2
3
4
5
-- Written by Mohit Jain
-- Count number of set bits in number
import Data.Bits

countSetBits ::Integer -> Integer
...
view (23 lines, 5 lines of output, 1 comment)
C++, pasted on Mar 22:
1
2
3
4
5
#include <cstdio>

class SR {
  int &ri;
  int v;
...
view (26 lines, 2 lines of output, 1 comment)
Haskell, pasted on Mar 22:
1
2
3
4
-- Written by Mohit Jain
-- Problem statement
-- Reverse every word of the string
-- (Convert multiple spaces into one space)
...
view (15 lines, 1 line of output)
Haskell, pasted on Mar 21:
1
2
3
4
5
-- Written by Mohit Jain
-- Print the maximum sum of a sub-list (contigous elements) in given list
-- Input may contain one or negative number
-- Input is empty or contains at least one positive number
main = printMaxSumOfSublist [4, -3, -2, 2, 3, -1, 3, -12, 5, -9, 0, -1]
...
view (15 lines, 1 line of output)
Haskell, pasted on Mar 20:
1
2
3
4
5
-- Written by Mohit jain
-- Read a file and prepend line number to each line
module Main where

import Control.Monad.State
...
view (19 lines, 3 lines of output, 2 comments)
Haskell, pasted on Mar 19:
1
2
3
4
5
-- Written by Mohit Jain
-- Input: A string containing only letters 'a'-'z'
-- Output: True if string is a valid name, False otherwise
-- Rules: A name is valid if it follows all the rules given below.
--        1. Length of name is exactly 8 characters.
...
view (23 lines, 10 lines of output, 1 comment)
C, pasted on Mar 16:
1
2
3
4
5
// http://codepad.org/udoDscmx
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
...
view (102 lines, 1 line of output, 1 comment)
C++, pasted on Mar 16:
1
2
3
4
5
#include <iostream>

// Verifying the problem
// CWD
// XOR AX,DX
...
view (18 lines, 31 lines of output)
C, pasted on Mar 16:
1
2
3
4
#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23, 34, 12, 17, 204, 99, 16};
...
view (13 lines)
Haskell, pasted on Mar 16:
1
2
3
4
5
-- Written by Mohit Jain
-- Calculate nth fibonacci number
main = printFibonacci 20

printFibonacci :: Int -> IO ()
...
view (13 lines, 1 line of output, 1 comment)
Haskell, pasted on Mar 15:
1
2
3
4
-- Written by Mohit Jain
-- Problem statement: Print all prime number in given range
main = showPrimeNumbersUpTo 4444 where
       showPrimeNumbersUpTo = putStrLn . unlines . printPrimes
...
view (13 lines, 604 lines of output, 1 comment)
Haskell, pasted on Mar 14:
1
2
3
4
-- Written by Mohit Jain
-- Problem statement: Print all prime number in given range
main = showPrimeNumbersUpTo 321 where
       showPrimeNumbersUpTo = putStrLn . unlines . printPrimes
...
view (12 lines, 67 lines of output)
C++, pasted on Mar 14:
1
2
3
4
5
// Print all prime numbers up to give number
// Algorithm: Wheel factorization
#include <iostream>
#include <vector>
#include <cassert>
...
view (53 lines, 9592 lines of output, 2 comments)
C++, pasted on Mar 14:
1
2
3
4
5
#include <iostream>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
...
view (48 lines, 12 lines of output)
C++, pasted on Mar 14:
1
2
3
4
5
#include <iostream>
#include <vector>
using namespace std;

/**
...
view (29 lines, 1107 lines of output)
C++, pasted on Mar 14:
1
2
3
4
5
// http://d.hatena.ne.jp/Nabetani/20111207/p1


#include <iostream>
#include <vector>
...
view (121 lines, 1 line of output)
C++, pasted on Mar 14:
1
2
3
4
5
// http://d.hatena.ne.jp/Nabetani/20111206/p1


#include <iostream>
#include <map>
...
view (56 lines, 1 line of output, 1 comment)
Haskell, pasted on Mar 13:
1
2
3
4
-- Written by Mohit Jain
-- Problem statement: Print list of roman numerals from 1 to 50

main = solveForList [1..50]
...
view (14 lines, 51 lines of output)
Haskell, pasted on Mar 13:
1
2
3
4
5
-- Written by Mohit Jain

-- Problem statement: Read input and insert comma between each word
-- Each word is separated by a space
-- Anything inside quotation is treated as single word
...
view (81 lines, 1 line of output, 1 comment)
C++, pasted on Mar 12:
1
2
3
4
5
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
...
view (22 lines, 100 lines of output)
C++, pasted on Mar 12:
1
2
3
4
#include <numeric>
#include <iostream>
#include <vector>
using namespace std;
...
view (14 lines, 1 line of output)
C, pasted on Mar 12:
1
2
3
4
5
f,t;main(i)
{
  for(i=1;i<101;++i)
    f=i%5,t=i%3,f&&t?printf("%d\n",i):puts(f&&!t?"fizz":"fizzbuzz"+4*!!t);
  return 0;
...
view (6 lines, 100 lines of output)
C, pasted on Mar 12:
1
2
3
4
5
f,t,r;main(i)
{
  char *fmt[] = {"%.*s\n", "%d\n"};
  for(i=1;i<101;++i) {
    f=i%5,t=i%3;
...
view (10 lines, 100 lines of output)
Haskell, pasted on Mar 12:
1
2
3
4
5
-- Author Mohit Jain
{-

== Problem statement ==
Implement a simplified version of scanf.
...
view (83 lines, 1 line of output)
Haskell, pasted on Mar 9:
1
2
3
4
-- Written by Mohit Jain

module Main where
import Data.List
...
view (32 lines, 1 line of output)
Haskell, pasted on Mar 9:
1
2
3
4
5
-- Author Mohit Jain

-- Problem statement
-- Input: A string of characters. All characters are in range [a-z]
-- Output: All characters separated by commas except
...
view (88 lines, 7 lines of output)
Haskell, pasted on Mar 7:
1
2
3
4
5
-- Written by Mohit to solve Pikachu problem
-- Pikachu is a character from Pokemon and he can speak
-- only 3 syllables: pi, ka and chu.
-- Aim of this program is to write a function that can verify
-- whether pikachu can speak a sentence or not
...
view (20 lines, 9 lines of output, 1 comment)
Haskell, pasted on Mar 5:
1
2
3
4
5
-- Written by Mohit Jain
module Main where

baseBall = throwBalls 0 0
throwBalls _ _ [] = []
...
view (14 lines, 23 lines of output, 1 comment)
C++, pasted on Mar 2:
1
2
3
4
#include <iostream>
#include <list>
#include <vector>
using namespace std;
...
view (22 lines, 7 lines of output)
Haskell, pasted on Feb 27:
1
2
3
4
5
-- Written by Mohit Jain
module Main where

-- Import List to enable sort
import List
...
view (48 lines, 43 lines of output, 1 comment)
Haskell, pasted on Feb 21:
1
2
3
4
5
module Main where

fizzList = take 100 (cycle ["", "", "fizz"])
buzzList = take 100 (cycle ["", "", "", "", "buzz"])
numList  = map show [1..100]
...
view (17 lines, 101 lines of output)
Haskell, pasted on Feb 21:
1
2
3
4
module Main where

tList = cycle [0, 0, 1]
fList = cycle [0, 0, 0, 0, 1]
...
view (14 lines, 101 lines of output)