|
codepad
|
|
|
Saved pastes by mohit_at_codepad:
/* 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) |
/* 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) |
/* 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) |
// 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) |
// 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) |
/* 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) |
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) |
-- 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) |
// 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) |
/// 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) |
// 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) |
/* 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) |
/**
* 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) |
/// 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) |
/// 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) |
// 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) |
/* 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) |
/**
* 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) |
// 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) |
-- 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) |
// 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) |
// Implement your own atoi
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
|
view (48 lines, 21 lines of output) |
// 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) |
/// 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) |
// Simple line drawing function
#include <stdio.h>
#include <math.h>
#define WIDTH 1000
|
view (43 lines, 42 lines of output) |
// Find the closest matching number from a binary search tree
#include <iostream>
#include <cassert>
using namespace std;
|
view (113 lines, 7 lines of output) |
#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) |
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
/// @brief Return first repeating character
|
view (43 lines, 27 lines of output) |
// 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) |
#include <string>
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
|
view (40 lines, 3 lines of output) |
// 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) |
// Reverse all vowels in a string
#include <stdio.h>
#include <string.h>
#include <ctype.h>
|
view (40 lines, 1 line of output) |
// 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) |
// 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) |
// 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) |
// 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) |
/// 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) |
/// 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) |
/// 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) |
/// Find pythagorus triplet in an array
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
|
view (43 lines, 28 lines of output) |
// 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) |
/// 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) |
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <algorithm>
#include <sys/time.h>
|
view (387 lines, 184 lines of output, 1 comment) |
/**
* 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) |
// Big numbers
// Author: Mohit Jain
#include <string>
#include <sstream>
#include <iostream>
|
view (149 lines, 1 line of output) |
-- 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) |
/* Print the value of pi */
#include <stdio.h>
#include <stdlib.h>
#if 0
#define const
|
view (24 lines, 1 line of output) |
// 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) |
/*
* 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) |
-- 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) |
-- 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) |
-- 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) |
-- 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) |
-- 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) |
-- 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) |
// 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) |
#include <stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23, 34, 12, 17, 204, 99, 16};
|
view (13 lines) |
-- Written by Mohit Jain
-- Calculate nth fibonacci number
main = printFibonacci 20
printFibonacci :: Int -> IO ()
|
view (13 lines, 1 line of output, 1 comment) |
-- 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) |
-- 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) |
// 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) |
#include <iostream>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
|
view (48 lines, 12 lines of output) |
-- 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) |
-- 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) |
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
|
view (22 lines, 100 lines of output) |
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) |
-- Author Mohit Jain
{-
== Problem statement ==
Implement a simplified version of scanf.
|
view (83 lines, 1 line of output) |
-- 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) |
-- 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) |
-- Written by Mohit Jain
module Main where
baseBall = throwBalls 0 0
throwBalls _ _ [] = []
|
view (14 lines, 23 lines of output, 1 comment) |
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) |