Creating a custom binary pattern from a given number - algorithm

Given n, I have a binary pattern to be generated like this in a part of my application:
n = 0
0 -> 0
n = 1
0 -> 0
1 -> 1
n = 2
0 -> 00
1 -> 01
2 -> 10
3 -> 11
n = 3
0 -> 000
1 -> 001
2 -> 010
3 -> 100
4 -> 011
5 -> 101
6 -> 110
7 -> 111
n = 4
0 -> 0000
1 -> 0001
2 -> 0010
3 -> 0100
4 -> 1000
5 -> 0011
6 -> 0101
7 -> 1001
8 -> 0110
9 -> 1010
10 -> 1100
11 -> 0111
12 -> 1011
13 -> 1101
14 -> 1110
15 -> 1111
n = 5
0 -> 00000
1 -> 00001
2 -> 00010
3 -> 00100
4 -> 01000
5 -> 10000
6 -> 00011
7 -> 00101
8 -> 01001
9 -> 10001
10 -> 00110
11 -> 01010
12 -> 10010
13 -> 01100
14 -> 10100
15 -> 11000
16 -> 00111
17 -> 01011
18 -> 10011
19 -> 01101
20 -> 10101
21 -> 11001
22 -> 01110
23 -> 10110
24 -> 11010
25 -> 11100
26 -> 01111
27 -> 10111
28 -> 11011
29 -> 11101
30 -> 11110
31 -> 11111
I'll try to explain this algorithm the best way I can:
The algorithm has loops. In each loop, an extra bit is flipped. Then combinations are to be made out of it.
So in the first loop, no bits are 1s.
In the second loop, only one bit is 1. We need to first go through all possible combinations, in such an order that the leftmost bits are lit only after all combinations for the rightmost bits are over.
Similarly keep proceeding to further loops.
I'm not sure how to write an efficient code for it. One thing I could think of is like a DP solution to this problem. But could there be a more elegant, something like a mathematical solution, where I could put in 'n' and get the binary pattern equivalent?

You could use a recursive approach. In the main routine, increase the number of one-bits you want to produce (from 1 to n), and then call a recursive function that will do that job as follows:
It chooses a bit to set to 1, and then calls the function recursively to use the remaining bits at the right of it, to place one fewer one-bits.
Here is an implementation in JavaScript, with a demo run for n=4:
function * generateOnes(numDigits, numOnes) {
if (numDigits === 0 || numOnes === 0) {
yield 0;
} else {
for (let pos = numOnes - 1; pos < numDigits; pos++) {
for (let result of generateOnes(pos, numOnes - 1)) {
yield (1 << pos) | result;
}
}
}
}
function * generate(numDigits) {
for (let numOnes = 1; numOnes <= numDigits; numOnes++) {
yield * generateOnes(numDigits, numOnes);
}
}
// Demo with n=4:
for (let result of generate(4)) {
console.log(result.toString(2).padStart(4, "0"));
}
Here is the equivalent in Python:
def generate_ones(num_digits, num_ones):
if num_digits == 0 or num_ones == 0:
yield 0
else:
for pos in range(num_ones - 1, num_digits):
for result in generate_ones(pos, num_ones - 1):
yield (1 << pos) | result
def generate(num_digits):
for num_ones in range(1, num_digits + 1):
yield from generate_ones(num_digits, num_ones)
# Demo with n=4:
for result in generate(4):
print('{0:04b}'.format(result))

n=int(input())
a=[]
for i in range(2**n):
Str = bin(i).replace('0b','')
a.append(Str)
for i in range(len(a)):
a[i] = '0'*(n-len(a[i])) + a[i]
for i in range(len(a)):
print(a[i])
If you have any doubts related to the code comment down

Supposing “We need to first go through all possible combinations, in such an order that the leftmost bits are lit only after all combinations for the rightmost bits are over” is correct and the example shown for n=4:
7 -> 1001
8 -> 0110
is wrong, then here is C code to iterate through the values as desired:
#include <stdio.h>
// Print the n-bit binary numeral for x.
static void PrintBinary(int n, unsigned x)
{
putchar('\t');
// Iterate through bit positions from high to low.
for (int p = n-1; 0 <= p; --p)
putchar('0' + ((x >> p) & 1));
putchar('\n');
}
/* This is from Hacker’s Delight by Henry S. Warren, Jr., 2003,
Addison-Wesley, Chapter 2 (“Basics”), Section 2-1 “Manipulating Rightmost
Bits”, page 14.
*/
static unsigned snoob(unsigned x)
{
/* Consider some bits in x dddd011...1100...00, where d is “do not care”
and there are t bits in that trailing group of 1s. Then, in the code
below:
smallest is set to the trailing 1 bit.
ripple adds to that bit, carrying to the next 0, producing
dddd100...0000...00. Note that t 1 bits changed to 0s and one 0
changed to 1, so ripple has t-1 fewer 1 bits than x does.
ones is set to all bits that changed, dddd111...1100...0. It has
t+1 bits set -- for the t 1s that changed to 0s and the 0 that
changed to 1.
ones/smallest aligns those bits to the right, leaving the lowest
t+1 bits set. Shifting right two bits leaves t-1 bits set.
Then ripple | ones restores t-1 1 bits in the low positions,
resulting in t bits set.
*/
unsigned smallest = x & -x; // Find trailing 1 bit.
unsigned ripple = x + smallest; // Change it, carrying to next 0.
unsigned ones = x ^ ripple; // Find all bits that changed.
ones = ones/smallest >> 2;
return ripple | ones;
}
/* Give a number of bits n, iterate through all values of n bits in order
first by the number of bits set then by the binary value.
*/
static void Iterate(int n)
{
printf("Patterns for n = %d:\n", n);
// Iterate s through the numbers of bits set.
for (int s = 0; s <= n; ++s)
{
/* Set s low bits. Note: If n can equal (or exceed) the number of
bits in unsigned, "1u << s" is not defined by the C standard, and
some alternative must be used.
*/
unsigned i = (1u << s) - 1;
// Find the highest value.
unsigned h = i << n-s;
PrintBinary(n, i);
while (i < h)
{
i = snoob(i);
PrintBinary(n, i);
}
}
}
int main(void)
{
for (int n = 1; n <= 4; ++n)
Iterate(n);
}

Related

Efficient code to load AVX vectors for 1D convolution kernel of length 8

An implementation of a 1D convolution operation will often need to load a vectors of data that sequentially step through a buffer of data offset by one element each iteration.
For example, consider a buffer of input data X[0], X[1], ..., X[n-1], where n is greater than twice the kernel length. If the convolution length is three, and we can fit eight elements in each vector, we might first want a vector with X[0], X[1], ..., X[7], then the next with X[1], X[2], ..., X[8] and the last with X[2], X[3], ..., X[9].
Consider the case where the kernel length as well as the vector length is 8. We must load eight vectors, that might look sequentially like this:
{ 0 1 2 3 4 5 6 7 }
{ 1 2 3 4 5 6 7 8 }
{ 2 3 4 5 6 7 8 9 }
{ 3 4 5 6 7 8 9 10 }
{ 4 5 6 7 8 9 10 11 }
{ 5 6 7 8 9 10 11 12 }
{ 6 7 8 9 10 11 12 13 }
{ 7 8 9 10 11 12 13 14 }
By reducing this sequence vertically, we could produce a running mean or sum. I.e., the sum of these vectors will have the sum of the first 8 elements in it's first position.
Consider that the order of the elements in the column does not matter. Any permutation of the elements in each column will still produce the same result. For a convolution, this permutation can be accounted for by altering the order of the constants used in the kernel.
Is there a faster way to load these vectors that takes advantage of this? Consider as a baseline the simple sequence of unaligned loads:
// Any sort of sliding window function, i.e. running mean, running max, convolution, etc.
void sliding_window(const float* input, unsigned length)
{
for (unsigned i = 0; i < length - 7; i += 8) {
for (unsigned j = 0; i < 8; j++) {
__m256 v = _mm256_loadu_ps(input[i + j]);
// reduction operation on v (e.g. max or fmadd) goes here
}
}
// handle tail here
}
First of all, you should note that if your convolution is separable, this is very often worth doing. Simple example:
res[i] = x[i]+x[i+1]+x[i+2]+x[i+3]+x[i+4]+x[i+5]+x[i+6]+x[i+7];
This can be done by convoluting with [1 1] * [1 0 1] * [1 0 0 0 1] in three steps, for example like so:
void sliding_window(float* output, const float* input, size_t length)
{
// Nomenclature
// aX input at i+X
// bX convolution with [1 1] starting at i+X
// cX convolution with [1 1] * [1 0 1] starting at i+X
// dX convolution with [1 1] * [1 0 1] * [1 0 0 0 1] starting at i+X
__m256 a0 = _mm256_load_ps(input), a8 = _mm256_load_ps(input + 8);
__m256 b0 = _mm256_add_ps(a0, _mm256_loadu_ps(input+1)), b8 = _mm256_add_ps(a8, _mm256_loadu_ps(input+9));
__m256 b4 = _mm256_permute2f128_ps(b0, b8, 1+16*2);
__m256 b2 = _mm256_shuffle_ps(b0, b4, 2+3*4+0*16+1*64);
__m256 c0 = _mm256_add_ps(b0, b2);
for (unsigned i = 0; i < length - 25; i += 8) {
// Convolute input with [1 1]
__m256 a16 = _mm256_load_ps( input + i + 16);
__m256 a17 = _mm256_loadu_ps(input + i + 17);
__m256 b16 = _mm256_add_ps(a16, a17);
// Convolute first convolution with [1 0 1]
__m256 b12 = _mm256_permute2f128_ps(b8, b16, 1+16*2);
__m256 b10 = _mm256_shuffle_ps(b8, b12, 2+3*4+0*16+1*64);
__m256 c8 = _mm256_add_ps(b8, b10);
// Convolute second convolution with [1 0 0 0 1]
__m256 c4 = _mm256_permute2f128_ps(c0, c8, 1+16*2);
__m256 d0 = _mm256_add_ps(c0, c4);
// Store result
_mm256_store_ps(output + i, d0);
// rename registers for next iteration:
b8 = b16;
c0 = c8;
}
// handle tail here ...
}
You can of course replace addps by maxps. Godbolt-Demo: https://godbolt.org/z/W9K9o943o
Overall, this takes 1 aligned + 1 unaligned load, 3 shuffles, 3 additions and 1 store for 8 elements (actually only using AVX1). On Intel CPUs with only 1 shuffle per cycle this may actually just be slightly faster than a naïve 8-load, 7-addition implementation (I did not benchmark this). On Zen3 I'm not sure about the actual cost of loading unaligned data.
If you have a non-trivial kernel it is probably hard to determine if it is separable, though.
The best I've been able to come up with is this sequence:
{ 0 1 2 3 4 5 6 7 }
{ 1 2 3 8 5 6 7 12 }
{ 2 3 8 9 6 7 12 13 }
{ 3 8 9 10 7 12 13 14 }
{ 4 5 6 7 8 9 10 11 }
{ 5 6 7 4 9 10 11 8 }
{ 6 7 4 5 10 11 8 9 }
{ 7 4 5 6 11 8 9 10 }
Each column contains the necessary elements. The first column contains 0 - 7, the next 1 - 8, then 2 - 9, etc.
This can be produced with the following sequence of operations:
void sliding_window(const float* input, unsigned length)
{
__m256 a = _mm256_load_ps(input);
for (unsigned i = 8; i < length - 7; i += 8) {
__m256 b = _mm256_load_ps(input + i);
__m256i ai = _mm256_castps_si256(a); // not part of sequence
__m256i bi = _mm256_castps_si256(b); // just for code reduction
// a is the first vector, these are remaining 7
__m256 j1 = _mm256_castsi256_ps(_mm256_alignr_epi8(bi, ai, 4));
// Reduction operation (add, fmadd, max, etc.) between a and j1 goes here
__m256 j2 = _mm256_castsi256_ps(_mm256_alignr_epi8(bi, ai, 8));
// Reduction with j2 goes here, and so on after each value
__m256 j3 = _mm256_castsi256_ps(_mm256_alignr_epi8(bi, ai, 12));
__m256 r0 = _mm256_permute2f128_ps(a, b, 0x21);
a = b; // Register with "b" isn't needed anymore
__m256 r1 = _mm256_permute_ps(r0, 0x39);
__m256 r2 = _mm256_permute_ps(r0, 0x4e);
__m256 r3 = _mm256_permute_ps(r0, 0x93);
// Final reduction with r3 to produce result
}
// handle tail here
}
On Zen3, I benchmark this as about 10% faster than the sequence of unaligned loads.

Find the closest integer with same weight O(1)

I am solving this problem:
The count of ones in binary representation of integer number is called the weight of that number. The following algorithm finds the closest integer with the same weight. For example, for 123 (0111 1011)₂, the closest integer number is 125 (0111 1101)₂.
The solution for O(n)
where n is the width of the input number is by swapping the positions of the first pair of consecutive bits that differ.
Could someone give me some hints for solving in it in O(1) runtime and space ?
Thanks
As already commented by ajayv this cannot really be done in O(1) as the answer always depends on the number of bits the input has. However, if we interpret the O(1) to mean that we have as an input some primitive integer data and all the logic and arithmetic operations we perform on that integer are O(1) (no loops over the bits), the problem can be solved in constant time. Of course, if we changed from 32bit integer to 64bit integer the running time would increase as the arithmetic operations would take longer on hardware.
One possible solution is to use following functions. The first gives you a number where only the lowest set bit of x is set
int lowestBitSet(int x){
( x & ~(x-1) )
}
and the second the lowest bit not set
int lowestBitNotSet(int x){
return ~x & (x+1);
}
If you work few examples of these on paper you see how they work.
Now you can find the bits you need to change using these two functions and then use the algorithm you already described.
A c++ implementation (not checking for cases where there are no answer)
unsigned int closestInt(unsigned int x){
unsigned int ns=lowestBitNotSet(x);
unsigned int s=lowestBitSet(x);
if (ns>s){
x|=ns;
x^=ns>>1;
}
else{
x^=s;
x|=s>>1;
}
return x;
}
To solve this problem in O(1) time complexity it can be considered that there are two main cases:
1) When LSB is '0':
In this case, the first '1' must be shifted with one position to the right.
Input : "10001000"
Out ::: "10000100"
2) When LSB is '1':
In this case the first '0' must be set to '1', and first '1' must be set to '0'.
Input : "10000111"
Out ::: "10001110"
The next method in Java represents one solution.
private static void findClosestInteger(String word) { // ex: word = "10001000"
System.out.println(word); // Print initial binary format of the number
int x = Integer.parseInt(word, 2); // Convert String to int
if((x & 1) == 0) { // Evaluates LSB value
// Case when LSB = '0':
// Input: x = 10001000
int firstOne = x & ~(x -1); // get first '1' position (from right to left)
// firstOne = 00001000
x = x & (x - 1); // set first '1' to '0'
// x = 10000000
x = x | (firstOne >> 1); // "shift" first '1' with one position to right
// x = 10000100
} else {
// Case when LSB = '1':
// Input: x = 10000111
int firstZero = ~x & ~(~x - 1); // get first '0' position (from right to left)
// firstZero = 00001000
x = x & (~1); // set first '1', which is the LSB, to '0'
// x = 10000110
x = x | firstZero; // set first '0' to '1'
// x = 10001110
}
for(int i = word.length() - 1; i > -1 ; i--) { // print the closest integer with same weight
System.out.print("" + ( ( (x & 1 << i) != 0) ? 1 : 0) );
}
}
The problem can be viewed as "which differing bits to swap in a bit representation of a number, so that the resultant number is closest to the original?"
So, if we we're to swap bits at indices k1 & k2, with k2 > k1, the difference between the numbers would be 2^k2 - 2^k1. Our goal is to minimize this difference. Assuming that the bit representation is not all 0s or all 1s, a simple observation yields that the difference would be least if we kept |k2 - k1| as minimum. The minimum value can be 1. So, if we're able to find two consecutive different bits, starting from the least significant bit (index = 0), our job is done.
The case where bits starting from Least Significant Bit to the right most set bit are all 1s
k2
|
7 6 5 4 3 2 1 0
---------------
n: 1 1 1 0 1 0 1 1
rightmostSetBit: 0 0 0 0 0 0 0 1
rightmostNotSetBit: 0 0 0 0 0 1 0 0 rightmostNotSetBit > rightmostSetBit so,
difference: 0 0 0 0 0 0 1 0 i.e. rightmostNotSetBit - (rightmostNotSetBit >> 1):
---------------
n + difference: 1 1 1 0 1 1 0 1
The case where bits starting from Least Significant Bit to the right most set bit are all 0s
k2
|
7 6 5 4 3 2 1 0
---------------
n: 1 1 1 0 1 1 0 0
rightmostSetBit: 0 0 0 0 0 1 0 0
rightmostNotSetBit: 0 0 0 0 0 0 0 1 rightmostSetBit > rightmostNotSetBit so,
difference: 0 0 0 0 0 0 1 0 i.e. rightmostSetBit -(rightmostSetBit>> 1)
---------------
n - difference: 1 1 1 0 1 0 1 0
The edge case, of course the situation where we have all 0s or all 1s.
public static long closestToWeight(long n){
if(n <= 0 /* If all 0s */ || (n+1) == Integer.MIN_VALUE /* n is MAX_INT */)
return -1;
long neg = ~n;
long rightmostSetBit = n&~(n-1);
long rightmostNotSetBit = neg&~(neg-1);
if(rightmostNotSetBit > rightmostSetBit){
return (n + (rightmostNotSetBit - (rightmostNotSetBit >> 1)));
}
return (n - (rightmostSetBit - (rightmostSetBit >> 1)));
}
Attempted the problem in Python. Can be viewed as a translation of Ari's solution with the edge case handled:
def closest_int_same_bit_count(x):
# if all bits of x are 0 or 1, there can't be an answer
if x & sys.maxsize in {sys.maxsize, 0}:
raise ValueError("All bits are 0 or 1")
rightmost_set_bit = x & ~(x - 1)
next_un_set_bit = ~x & (x + 1)
if next_un_set_bit > rightmost_set_bit:
# 0 shifted to the right e.g 0111 -> 1011
x ^= next_un_set_bit | next_un_set_bit >> 1
else:
# 1 shifted to the right 1000 -> 0100
x ^= rightmost_set_bit | rightmost_set_bit >> 1
return x
Similarly jigsawmnc's solution is provided below:
def closest_int_same_bit_count(x):
# if all bits of x are 0 or 1, there can't be an answer
if x & sys.maxsize in {sys.maxsize, 0}:
raise ValueError("All bits are 0 or 1")
rightmost_set_bit = x & ~(x - 1)
next_un_set_bit = ~x & (x + 1)
if next_un_set_bit > rightmost_set_bit:
# 0 shifted to the right e.g 0111 -> 1011
x += next_un_set_bit - (next_un_set_bit >> 1)
else:
# 1 shifted to the right 1000 -> 0100
x -= rightmost_set_bit - (rightmost_set_bit >> 1)
return x
Java Solution:
//Swap the two rightmost consecutive bits that are different
for (int i = 0; i < 64; i++) {
if ((((x >> i) & 1) ^ ((x >> (i+1)) & 1)) == 1) {
// then swap them or flip their bits
int mask = (1 << i) | (1 << i + 1);
x = x ^ mask;
System.out.println("x = " + x);
return;
}
}
static void findClosestIntWithSameWeight(uint x)
{
uint xWithfirstBitSettoZero = x & (x - 1);
uint xWithOnlyfirstbitSet = x & ~(x - 1);
uint xWithNextTofirstBitSet = xWithOnlyfirstbitSet >> 1;
uint closestWeightNum = xWithfirstBitSettoZero | xWithNextTofirstBitSet;
Console.WriteLine("Closet Weight for {0} is {1}", x, closestWeightNum);
}
Code in python:
def closest_int_same_bit_count(x):
if (x & 1) != ((x >> 1) & 1):
return x ^ 0x3
diff = x ^ (x >> 1)
rbs = diff & ~(diff - 1)
i = int(math.log(rbs, 2))
return x ^ (1 << i | 1 << i + 1)
A great explanation of this problem can be found on question 4.4 in EPI.
(Elements of Programming Interviews)
Another place would be this link on geeksforgeeks.org if you don't own the book.
(Time complexity may be wrong on this link)
Two things you should keep in mind here is (Hint if you're trying to solve this for yourself):
You can use x & (x - 1) to clear the lowest set-bit (not to get confused with LSB - least significant bit)
You can use x & ~(x - 1) to get/extract the lowest set bit
If you know the O(n) solution you know that we need to find the index of the first bit that differs from LSB.
If you don't know what the LBS is:
0000 0000
^ // it's bit all the way to the right of a binary string.
Take the base two number 1011 1000 (184 in decimal)
The first bit that differs from LSB:
1011 1000
^ // this one
We'll record this as K1 = 0000 1000
Then we need to swap it with the very next bit to the right:
0000 1000
^ // this one
We'll record this as K2 = 0000 0100
Bitwise OR K1 and K2 together and you'll get a mask
mask = K1 | k2 // 0000 1000 | 0000 0100 -> 0000 1100
Bitwise XOR the mask with the original number and you'll have the correct output/swap
number ^ mask // 1011 1000 ^ 0000 1100 -> 1011 0100
Now before we pull everything together we have to consider that fact that the LSB could be 0001, and so could a bunch of bits after that 1000 1111. So we have to deal with the two cases of the first bit that differs from the LSB; it may be a 1 or 0.
First we have a conditional that test the LSB to be 1 or 0: x & 1
IF 1 return x XORed with the return of a helper function
This helper function has a second argument which its value depends on whether the condition is true or not. func(x, 0xFFFFFFFF) // if true // 0xFFFFFFFF 64 bit word with all bits set to 1
Otherwise we'll skip the if statement and return a similar expression but with a different value provided to the second argument.
return x XORed with func(x, 0x00000000) // 64 bit word with all bits set to 0. You could alternatively just pass 0 but I did this for consistency
Our helper function returns a mask that we are going to XOR with the original number to get our output.
It takes two arguments, our original number and a mask, used in this expression:
(x ^ mask) & ~((x ^ mask) - 1)
which gives us a new number with the bit at index K1 always set to 1.
It then shifts that bit 1 to the right (i.e index K2) then ORs it with itself to create our final mask
0000 1000 >> 1 -> 0000 0100 | 0001 0000 -> 0000 1100
This all implemented in C++ looks like:
unsigned long long int closestIntSameBitCount(unsigned long long int n)
{
if (n & 1)
return n ^= getSwapMask(n, 0xFFFFFFFF);
return n ^= getSwapMask(n, 0x00000000);
}
// Helper function
unsigned long long int getSwapMask(unsigned long long int n, unsigned long long int mask)
{
unsigned long long int swapBitMask = (n ^ mask) & ~((n ^ mask) - 1);
return swapBitMask | (swapBitMask >> 1);
}
Keep note of the expression (x ^ mask) & ~((x ^ mask) - 1)
I'll now run through this code with my example 1011 1000:
// start of closestIntSameBitCount
if (0) // 1011 1000 & 1 -> 0000 0000
// start of getSwapMask
getSwapMask(1011 1000, 0x00000000)
swapBitMask = (x ^ mask) & ~1011 0111 // ((x ^ mask) - 1) = 1011 1000 ^ .... 0000 0000 -> 1011 1000 - 1 -> 1011 0111
swapBitMask = (x ^ mask) & 0100 1000 // ~1011 0111 -> 0100 1000
swapBitMask = 1011 1000 & 0100 1000 // (x ^ mask) = 1011 1000 ^ .... 0000 0000 -> 1011 1000
swapBitMask = 0000 1000 // 1011 1000 & 0100 1000 -> 0000 1000
return swapBitMask | 0000 0100 // (swapBitMask >> 1) = 0000 1000 >> 1 -> 0000 0100
return 0000 1100 // 0000 1000 | 0000 0100 -> 0000 11000
// end of getSwapMask
return 1011 0100 // 1011 1000 ^ 0000 11000 -> 1011 0100
// end of closestIntSameBitCount
Here is a full running example if you would like compile and run it your self:
#include <iostream>
#include <stdio.h>
#include <bitset>
unsigned long long int closestIntSameBitCount(unsigned long long int n);
unsigned long long int getSwapMask(unsigned long long int n, unsigned long long int mask);
int main()
{
unsigned long long int number;
printf("Pick a number: ");
std::cin >> number;
std::bitset<64> a(number);
std::bitset<64> b(closestIntSameBitCount(number));
std::cout << a
<< "\n"
<< b
<< std::endl;
}
unsigned long long int closestIntSameBitCount(unsigned long long int n)
{
if (n & 1)
return n ^= getSwapMask(n, 0xFFFFFFFF);
return n ^= getSwapMask(n, 0x00000000);
}
// Helper function
unsigned long long int getSwapMask(unsigned long long int n, unsigned long long int mask)
{
unsigned long long int swapBitMask = (n ^ mask) & ~((n ^ mask) - 1);
return swapBitMask | (swapBitMask >> 1);
}
This was my solution to the problem. I guess #jigsawmnc explains pretty well why we need to have |k2 -k1| to a minimum. So in order to find the closest integer, with the same weight, we would want to find the location where consecutive bits are flipped and then flip them again to get the answer. In order to do that we can shift the number 1 unit. Take the XOR with the same number. This will set bits at all locations where there is a flip. Find the least significant bit for the XOR. This will give you the smallest location to flip. Create a mask for the location and next bit. Take an XOR and that should be the answer. This won't work, if the digits are all 0 or all 1
Here is the code for it.
def variant_closest_int(x: int) -> int:
if x == 0 or ~x == 0:
raise ValueError('All bits are 0 or 1')
x_ = x >> 1
lsb = x ^ x_
mask_ = lsb & ~(lsb - 1)
mask = mask_ | (mask_ << 1)
return x ^ mask
My solution, takes advantage of the parity of the integer. I think the way I got the LSB masks can be simplified
def next_weighted_int(x):
if x % 2 == 0:
lsb_mask = ( ((x - 1) ^ x) >> 1 ) + 1 # Gets a mask for the first 1
x ^= lsb_mask
x |= (lsb_mask >> 1)
return x
lsb_mask = ((x ^ (x + 1)) >> 1 ) + 1 # Gets a mask for the first 0
x |= lsb_mask
x ^= (lsb_mask >> 1)
return x
Just sharing my python solution for this problem:
def same closest_int_same_bit_count(a):
x = a + (a & 1) # change last bit to 0
bit = (x & ~(x-1)) # get last set bit
return a ^ (bit | bit >> 1) # swap set bit with unset bit
func findClosestIntegerWithTheSameWeight2(x int) int {
rightMost0 := ^x & (x + 1)
rightMost1 := x & (-x)
if rightMost0 > 1 {
return (x ^ rightMost0) ^ (rightMost0 >> 1)
} else {
return (x ^ rightMost1) ^ (rightMost1 >> 1)
}
}

all integers which are subsequences of an integer (considered as a string of digits)

Given an integer 1<=n<=100000. How do I get efficiently all the integers which are subsequences of the integer n (considering the integer n as a string of digits). I want a pseudo code for this.
eg. input: n=121, output: 1,12,11,2,21 (the order of output has no importance)
eg. input: n=132, output: 1,13,12,3,32,2
thanks in advance
Here's another version, using recursion. This one uses just integer division and modulo (both combined in divmod) to get the 'sub-integers'. It's done in Python, which is just as good as pseudo code...
def subints(n):
d, r = divmod(n, 10)
if d > 0:
for s in subints(d):
yield 10*s + r
yield s
yield r
Example: (a set of the resulting numbers would be enough; using list for better understanding)
>>> print list(subints(1234))
[1234, 123, 124, 12, 134, 13, 14, 1, 234, 23, 24, 2, 34, 3, 4]
n=121
length = 3
Generate all possible binary numbers of length 3. See the set bits in binary number and print the corresponding digit from the original number.
Example 1
121 length=3
000 -> 0 (ignore this:no bit set)
001 -> 1 (__1)
010 -> 2 (_2_)
011 -> 21 (_21)
100 -> 1 (repeated: ignore this)
101 -> 11 (1_1)
110 -> 12 (12_)
111 -> 121 (121)
Example 2: n=1234
1234 length=4
0000 -> 0 (ignore this:no bit set)
0001 -> 4 (___4)
0010 -> 3 (__3_)
0011 -> 34 (__34)
0100 -> 2 (_2__)
0101 -> 24 (_2_4)
0110 -> 23 (_23_)
0111 -> 234 (_234)
1000 -> 1 (1___)
1001 -> 14 (1__4)
1010 -> 13 (1_3_)
1011 -> 134 (1_34)
1100 -> 12 (12__)
1101 -> 124 (12_4)
1110 -> 123 (123_)
1111 -> 1234 (1234)
The code for the above algorithm made by me in C is pasted below. I have not performed many optimizations but the logic is same.
Sorry for the unoptimized code.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 30
main()
{
int i,j,end,index,k,swap;
char str[SIZE],arr[SIZE];
char buffer[SIZE],dict[SIZE][SIZE]={'\0'}; //dictlength is used to store the
int dictlength=0;
gets(str);
for(i=1;i<pow(2,strlen(str));i++)
{
index=0;
end=strlen(str);
itoa(i,arr,2);
for(j=strlen(arr);j>=0;j--,end--)
{
if(arr[j]=='1')
{
buffer[index]=str[end];
index=index+1;
}
}
buffer[index]='\0';
for(k=0,j=strlen(buffer)-1; k<j ; k++,j--)
{
swap=buffer[k];
buffer[k]=buffer[j];
buffer[j]=swap;
}
strcpy(dict[dictlength],buffer);
dictlength++;
}
for(i=0;i<dictlength;i++)
puts(dict[i]);
}
How about this:
static HashSet<string> Subseq(string input, HashSet<string> result = null, int pos = 0, string current = "")
{
if (result == null)
{
result = new HashSet<string>();
}
if (pos == input.Length)
{
return result;
}
Subseq(input, result, pos + 1, current);
current += input[pos];
if (!result.Contains(current))
{
result.Add(current);
}
Subseq(input, result, pos + 1, current);
return result;
}
Called as:
var result = Subseq("18923928");
If you don't consider "123" a subsequence of "123", then just add the condition current != input to the last if.

Code Golf: Gray Code

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
The Challenge
The shortest program by character count that outputs the n-bit Gray Code. n will be an arbitrary number smaller than 1000100000 (due to user suggestions) that is taken from standard input. The gray code will be printed in standard output, like in the example.
Note: I don't expect the program to print the gray code in a reasonable time (n=100000 is overkill); I do expect it to start printing though.
Example
Input:
4
Expected Output:
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Python - 53 chars
n=1<<input()
for x in range(n):print bin(n+x^x/2)[3:]
This 54 char version overcomes the limitation of range in Python2 so n=100000 works!
x,n=0,1<<input()
while n>x:print bin(n+x^x/2)[3:];x+=1
69 chars
G=lambda n:n and[x+y for x in'01'for y in G(n-1)[::1-2*int(x)]]or['']
75 chars
G=lambda n:n and['0'+x for x in G(n-1)]+['1'+x for x in G(n-1)[::-1]]or['']
APL (29 chars)
With the function F as (⌽ is the 'rotate' char)
z←x F y
z←(0,¨y),1,¨⌽y
This produces the Gray Code with 5 digits (⍴ is now the 'rho' char)
F/5⍴⊂0,1
The number '5' can be changed or be a variable.
(Sorry about the non-printable APL chars. SO won't let me post images as a new user)
Impossible! language (54 58 chars)
#l{'0,'1}1[;#l<][%;~['1%+].>.%['0%+].>.+//%1+]<>%[^].>
Test run:
./impossible gray.i! 5
Impossible v0.1.28
00000
00001
00011
00010
00110
00111
00101
00100
01100
01101
01111
01110
01010
01011
01001
01000
11000
11001
11011
11010
11110
11111
11101
11100
10100
10101
10111
10110
10010
10011
10001
10000
(actually I don't know if personal languages are allowed, since Impossible! is still under development, but I wanted to post it anyway..)
Golfscript - 27 chars
Reads from stdin, writes to stdout
~2\?:),{.2/^)+2base''*1>n}%
Sample run
$ echo 4 | ruby golfscript.rb gray.gs
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
Ruby - 49 chars
(1<<n=gets.to_i).times{|x|puts"%.#{n}b"%(x^x/2)}
This works for n=100000 with no problem
C++, 168 characters, not including whitespaces:
#include <iostream>
#include <string>
int r;
void x(std::string p, char f=48)
{
if(!r--)std::cout<<p<<'\n';else
{x(p+f);x(p+char(f^1),49);}
r++;
}
int main() {
std::cin>>r;
x("");
return 0;
}
Haskell, 82 characters:
f a=map('0':)a++map('1':)(reverse a)
main=interact$unlines.(iterate f[""]!!).read
Point-free style for teh win! (or at least 4 fewer strokes). Kudos to FUZxxl.
previous: 86 characters:
f a=map('0':)a++map('1':)(reverse a)
main=interact$ \s->unlines$iterate f[""]!!read s
Cut two strokes with interact, one with unlines.
older: 89 characters:
f a=map('0':)a++map('1':)(reverse a)
main=readLn>>= \s->putStr$concat$iterate f["\n"]!!s
Note that the laziness gets you your immediate output for free.
Mathematica 50 Chars
Nest[Join["0"<>#&/##,"1"<>#&/#Reverse##]&,{""},#]&
Thanks to A. Rex for suggestions!
Previous attempts
Here is my attempt in Mathematica (140 characters). I know that it isn't the shortest, but I think it is the easiest to follow if you are familiar with functional programming (though that could be my language bias showing). The addbit function takes an n-bit gray code and returns an n+1 bit gray code using the logic from the wikipedia page.. The make gray code function applies the addbit function in a nested manner to a 1 bit gray code, {{0}, {1}}, until an n-bit version is created. The charactercode function prints just the numbers without the braces and commas that are in the output of the addbit function.
addbit[set_] :=
Join[Map[Prepend[#, 0] &, set], Map[Prepend[#, 1] &, Reverse[set]]]
MakeGray[n_] :=
Map[FromCharacterCode, Nest[addbit, {{0}, {1}}, n - 1] + 48]
Straightforward Python implementation of what's described in Constructing an n-bit Gray code on Wikipedia:
import sys
def _gray(n):
if n == 1:
return [0, 1]
else:
p = _gray(n-1)
pr = [x + (1<<(n-1)) for x in p[::-1]]
return p + pr
n = int(sys.argv[1])
for i in [("0"*n + bin(a)[2:])[-n:] for a in _gray(n)]:
print i
(233 characters)
Test:
$ python gray.py 4
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000
C, 203 Characters
Here's a sacrificial offering, in C:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char s[256];
int b, i, j, m, g;
gets(s);
b = atoi(s);
for (i = 0; i < 1 << b; ++i)
{
g = i ^ (i / 2);
m = 1 << (b - 1);
for (j = 0; j < b; ++j)
{
s[j] = (g & m) ? '1' : '0';
m >>= 1;
}
s[j] = '\0';
puts(s);
}
return 0;
}
C#, 149143 characters
C# isn't the best language for code golf, but I thought I'd go at it anyway.
static void Main(){var s=1L<<int.Parse(Console.ReadLine());for(long i=0;i<s;i++){Console.WriteLine(Convert.ToString(s+i^i/2,2).Substring(1));}}
Readable version:
static void Main()
{
var s = 1L << int.Parse(Console.ReadLine());
for (long i = 0; i < s; i++)
{
Console.WriteLine(Convert.ToString(s + i ^ i / 2, 2).Substring(1));
}
}
And here is my Fantom sacrificial offering
public static Str[]grayCode(Int i){if(i==1)return["0","1"];else{p:=grayCode(i-1);p.addAll(p.dup.reverse);p.each|s,c|{if(c<(p.size/2))p[c]="0"+s;else p[c]="1"+s;};return p}}
(177 char)
Or the expanded version:
public static Str[] grayCode(Int i)
{
if (i==1) return ["0","1"]
else{
p := grayCode(i-1);
p.addAll(p.dup.reverse);
p.each |s,c|
{
if(c<(p.size/2))
{
p[c] = "0" + s
}
else
{
p[c] = "1" + s
}
}
return p
}
}
F#, 152 characters
let m=List.map;;let rec g l=function|1->l|x->g((m((+)"0")l)#(l|>List.rev|>m((+)"1")))(x - 1);;stdin.ReadLine()|>int|>g["0";"1"]|>List.iter(printfn "%s")
F# 180 175 too many characters
This morning I did another version, simplifying the recursive version, but alas due to recursion it wouldn't do the 100000.
Recursive solution:
let rec g m n l =
if(m = n) then l
else List.map ((+)"0") l # List.map ((+)"1") (List.rev(l)) |> g (m+1) n
List.iter (fun x -> printfn "%s" x) (g 1 (int(stdin.ReadLine())) ["0";"1"]);;
After that was done I created a working version for the "100000" requirement - it's too long to compete with the other solutions shown here and I probably re-invented the wheel several times over, but unlike many of the solutions I have seen here it will work with a very,very large number of bits and hey it was a good learning experience for an F# noob - I didn't bother to shorten it, since it's way too long anyway ;-)
Iterative solution: (working with 100000+)
let bits = stdin.ReadLine() |>int
let n = 1I <<< bits
let bitcount (n : bigint) =
let mutable m = n
let mutable c = 1
while m > 1I do
m <- m >>>1
c<-c+1
c
let rec traverseBits m (number: bigint) =
let highbit = bigint(1 <<< m)
if m > bitcount number
then number
else
let lowbit = 1 <<< m-1
if (highbit&&& number) > 0I
then
let newnum = number ^^^ bigint(lowbit)
traverseBits (m+1) newnum
else traverseBits (m+1) number
let res = seq
{
for i in 0I..n do
yield traverseBits 1 i
}
let binary n m = seq
{
for i = m-1 downto 0 do
let bit = bigint(1 <<< i)
if bit &&&n > 0I
then yield "1"
else yield "0"
}
Seq.iter (fun x -> printfn "%s" (Seq.reduce (+) (binary x bits))) res
Lua, 156 chars
This is my throw at it in Lua, as close as I can get it.
LuaJIT (or lua with lua-bitop): 156 bytes
a=io.read()n,w,b=2^a,io.write,bit;for x=0,n-1 do t=b.bxor(n+x,b.rshift(x,1))for k=a-1,0,-1 do w(t%2^k==t%n and 0 or 1)t=t%2^k==t and t or t%2^k end w'\n'end
Lua 5.2: 154 bytes
a=io.read()n,w,b=2^a,io.write,bit32;for x=0,n-1 do t=b.XOR(n+x,b.SHR(x,1))for k=a-1,0,-1 do w(t%2^k==t%n and 0 or 1)t=t%2^k==t and t or t%2^k end w'\n'end
In cut-free Prolog (138 bytes if you remove the space after '<<'; submission editor truncates the last line without it):
b(N,D):-D=0->nl;Q is D-1,H is N>>Q/\1,write(H),b(N,Q).
c(N,D):-N=0;P is N xor(N//2),b(P,D),M is N-1,c(M,D).
:-read(N),X is 1<< N-1,c(X,N).
Ruby, 50 Chars
(2**n=gets.to_i).times{|i|puts"%0#{n}d"%i.to_s(2)}

Code-golf: generate pascal's triangle

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible!
Here goes my attempt (118 characters in python 2.6 using a trick):
c,z,k=locals,[0],'_[1]'
p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)]
Explanation:
the first element of the list comprehension (when the length is 0) is [1]
the next elements are obtained the following way:
take the previous list and make two lists, one padded with a 0 at the beginning and the other at the end.
e.g. for the 2nd step, we take [1] and make [0,1] and [1,0]
sum the two new lists element by element
e.g. we make a new list [(0,1),(1,0)] and map with sum.
repeat n times and that's all.
usage (with pretty printing, actually out of the code-golf xD):
result = p(10)
lines = [" ".join(map(str, x)) for x in result]
for i in lines:
print i.center(max(map(len, lines)))
output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
K (Wikipedia), 15 characters:
p:{x{+':x,0}\1}
Example output:
p 10
(1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1)
It's also easily explained:
p:{x {+':x,0} \ 1}
^ ^------^ ^ ^
A B C D
p is a function taking an implicit parameter x.
p unfolds (C) an anonymous function (B) x times (A) starting at 1 (D).
The anonymous function simply takes a list x, appends 0 and returns a result by adding (+) each adjacent pair (':) of values: so e.g. starting with (1 2 1), it'll produce (1 2 1 0), add pairs (1 1+2 2+1 1+0), giving (1 3 3 1).
Update: Adapted to K4, which shaves off another two characters. For reference, here's the original K3 version:
p:{x{+':0,x,0}\1}
J, another language in the APL family, 9 characters:
p=:!/~#i.
This uses J's builtin "combinations" verb.
Output:
p 10
1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
0 0 1 3 6 10 15 21 28 36
0 0 0 1 4 10 20 35 56 84
0 0 0 0 1 5 15 35 70 126
0 0 0 0 0 1 6 21 56 126
0 0 0 0 0 0 1 7 28 84
0 0 0 0 0 0 0 1 8 36
0 0 0 0 0 0 0 0 1 9
0 0 0 0 0 0 0 0 0 1
Haskell, 58 characters:
r 0=[1]
r(n+1)=zipWith(+)(0:r n)$r n++[0]
p n=map r[0..n]
Output:
*Main> p 5
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]
More readable:
-- # row 0 is just [1]
row 0 = [1]
-- # row (n+1) is calculated from the previous row
row (n+1) = zipWith (+) ([0] ++ row n) (row n ++ [0])
-- # use that for a list of the first n+1 rows
pascal n = map row [0..n]
69C in C:
f(int*t){int*l=t+*t,*p=t,r=*t,j=0;for(*t=1;l<t+r*r;j=*p++)*l++=j+*p;}
Use it like so:
int main()
{
#define N 10
int i, j;
int t[N*N] = {N};
f(t);
for (i = 0; i < N; i++)
{
for (j = 0; j <= i; j++)
printf("%d ", t[i*N + j]);
putchar('\n');
}
return 0;
}
F#: 81 chars
let f=bigint.Factorial
let p x=[for n in 0I..x->[for k in 0I..n->f n/f k/f(n-k)]]
Explanation: I'm too lazy to be as clever as the Haskell and K programmers, so I took the straight forward route: each element in Pascal's triangle can be uniquely identified using a row n and col k, where the value of each element is n!/(k! (n-k)!.
Python: 75 characters
def G(n):R=[[1]];exec"R+=[map(sum,zip(R[-1]+[0],[0]+R[-1]))];"*~-n;return R
Shorter prolog version (112 instead of 164):
n([X],[X]).
n([H,I|T],[A|B]):-n([I|T],B),A is H+I.
p(0,[[1]]):-!.
p(N,[R,S|T]):-O is N-1,p(O,[S|T]),n([0|S],R).
another stab (python):
def pascals_triangle(n):
x=[[1]]
for i in range(n-1):
x.append(list(map(sum,zip([0]+x[-1],x[-1]+[0]))))
return x
Haskell, 164C with formatting:
i l=zipWith(+)(0:l)$l++[0]
fp=map (concatMap$(' ':).show)f$iterate i[1]
c n l=if(length l<n)then c n$' ':l++" "else l
cl l=map(c(length$last l))l
pt n=cl$take n fp
Without formatting, 52C:
i l=zipWith(+)(0:l)$l++[0]
pt n=take n$iterate i[1]
A more readable form of it:
iterateStep row = zipWith (+) (0:row) (row++[0])
pascalsTriangle n = take n $ iterate iterateStep [1]
-- For the formatted version, we reduce the number of rows at the final step:
formatRow r = concatMap (\l -> ' ':(show l)) r
formattedLines = map formatRow $ iterate iterateStep [1]
centerTo width line =
if length line < width
then centerTo width (" " ++ line ++ " ")
else line
centerLines lines = map (centerTo (length $ last lines)) lines
pascalsTriangle n = centerLines $ take n formattedLines
And perl, 111C, no centering:
$n=<>;$p=' 1 ';for(1..$n){print"$p\n";$x=" ";while($p=~s/^(?= ?\d)(\d* ?)(\d* ?)/$2/){$x.=($1+$2)." ";}$p=$x;}
Scheme — compressed version of 100 characters
(define(P h)(define(l i r)(if(> i h)'()(cons r(l(1+ i)(map +(cons 0 r)(append r '(0))))))(l 1 '(1)))
This is it in a more readable form (269 characters):
(define (pascal height)
(define (next-row row)
(map +
(cons 0 row)
(append row '(0))))
(define (iter i row)
(if (> i height)
'()
(cons row
(iter (1+ i)
(next-row row)))))
(iter 1 '(1)))
VBA/VB6 (392 chars w/ formatting)
Public Function PascalsTriangle(ByVal pRows As Integer)
Dim iRow As Integer
Dim iCol As Integer
Dim lValue As Long
Dim sLine As String
For iRow = 1 To pRows
sLine = ""
For iCol = 1 To iRow
If iCol = 1 Then
lValue = 1
Else
lValue = lValue * (iRow - iCol + 1) / (iCol - 1)
End If
sLine = sLine & " " & lValue
Next
Debug.Print sLine
Next
End Function
PHP 100 characters
$v[]=1;while($a<34){echo join(" ",$v)."\n";$a++;for($k=0;$k<=$a;$k++)$t[$k]=$v[$k-1]+$v[$k];$v=$t;}
Ruby, 83c:
def p(n);n>0?(m=p(n-1);k=m.last;m+[([0]+k).zip(k+[0]).map{|x|x[0]+x[1]}]):[[1]];end
test:
irb(main):001:0> def p(n);n>0?(m=p(n-1);k=m.last;m+[([0]+k).zip(k+[0]).map{|x|x[0]+x[1]}]):[[1]];end
=> nil
irb(main):002:0> p(5)
=> [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
irb(main):003:0>
Another python solution, that could be much shorter if the builtin functions had shorter names... 106 characters.
from itertools import*
r=range
p=lambda n:[[len(list(combinations(r(i),j)))for j in r(i+1)]for i in r(n)]
Another try, in prolog (I'm practising xD), not too short, just 164c:
s([],[],[]).
s([H|T],[J|U],[K|V]):-s(T,U,V),K is H+J.
l([1],0).
l(P,N):-M is N-1,l(A,M),append(A,[0],B),s(B,[0|A],P).
p([],-1).
p([H|T],N):-M is N-1,l(H,N),p(T,M).
explanation:
s = sum lists element by element
l = the Nth row of the triangle
p = the whole triangle of size N
VBA, 122 chars:
Sub p(n)
For r = 1 To n
l = "1"
v = 1
For c = 1 To r - 1
v = v / c * (r - c)
l = l & " " & v
Next
Debug.Print l
Next
End Sub
I wrote this C++ version a few years ago:
#include <iostream>
int main(int,char**a){for(int b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;b<atoi(a[1]);(d|f|h)>1?e*=d>1?--d:1,g*=f>1?--f:1,i*=h>1?--h:1:((std::cout<<(i*g?e/(i*g):1)<<" "?d=b+=c++==b?c=0,std::cout<<std::endl?1:0:0,h=d-(f=c):0),e=d,g=f,i=h));}
The following is just a Scala function returning a List[List[Int]]. No pretty printing or anything. Any suggested improvements? (I know it's inefficient, but that's not the main challenge now, is it?). 145 C.
def p(n: Int)={def h(n:Int):List[Int]=n match{case 1=>1::Nil;case _=>(0::h(n-1) zipAll(h(n-1),0,0)).map{n=>n._1+n._2}};(1 to n).toList.map(h(_))}
Or perhaps:
def pascal(n: Int) = {
def helper(n: Int): List[Int] = n match {
case 1 => 1 :: List()
case _ => (0 :: helper(n-1) zipAll (helper(n-1),0,0)).map{ n => n._1 + n._2 }
}
(1 to n).toList.map(helper(_))
}
(I'm a Scala noob, so please be nice to me :D )
a Perl version (139 chars w/o shebang)
#p = (1,1);
while ($#p < 20) {
#q =();
$z = 0;
push #p, 0;
foreach (#p) {
push #q, $_+$z;
$z = $_
}
#p = #q;
print "#p\n";
}
output starts from 1 2 1
PHP, 115 chars
$t[][]=1;
for($i=1;$i<$n;++$i){
$t[$i][0]=1;
for($j=1;$j<$i;++$j)$t[$i][$j]=$t[$i-1][$j-1]+$t[$i-1][$j];
$t[$i][$i]=1;}
If you don't care whether print_r() displays the output array in the correct order, you can shave it to 113 chars like
$t[][]=1;
for($i=1;$i<$n;++$i){
$t[$i][0]=$t[$i][$i]=1;
for($j=1;$j<$i;++$j)$t[$i][$j]=$t[$i-1][$j-1]+$t[$i-1][$j];}
Perl, 63 characters:
for(0..9){push#z,1;say"#z";#z=(1,map{$z[$_-1]+$z[$_]}(1..$#z))}
My attempt in C++ (378c). Not anywhere near as good as the rest of the posts.. but I'm proud of myself for coming up with a solution on my own =)
int* pt(int n)
{
int s=n*(n+1)/2;
int* t=new int[s];
for(int i=0;i<n;++i)
for(int j=0;j<=i;++j)
t[i*n+j] = (!j || j==i) ? 1 : t[(i-1)*n+(j-1)] + t[(i-1)*n+j];
return t;
}
int main()
{
int n,*t;
std::cin>>n;
t=pt(n);
for(int i=0;i<n;++i)
{
for(int j=0;j<=i;j++)
std::cout<<t[i*n+j]<<' ';
std::cout<<"\n";
}
}
Old thread, but I wrote this in response to a challenge on another forum today:
def pascals_triangle(n):
x=[[1]]
for i in range(n-1):
x.append([sum(i) for i in zip([0]+x[-1],x[-1]+[0])])
return x
for x in pascals_triangle(5):
print('{0:^16}'.format(x))
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]

Resources