From a33e2c826eccdeca1575ac4baec91b4602933040 Mon Sep 17 00:00:00 2001 From: Sudhanshu Kumar <35608688+Sudhansan5@users.noreply.github.com> Date: Tue, 6 Oct 2020 16:22:46 +0530 Subject: [PATCH] Add files via upload --- BitManipulation/Count set bits_1 to n.ipynb | 62 +++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 BitManipulation/Count set bits_1 to n.ipynb diff --git a/BitManipulation/Count set bits_1 to n.ipynb b/BitManipulation/Count set bits_1 to n.ipynb new file mode 100644 index 0000000..7368068 --- /dev/null +++ b/BitManipulation/Count set bits_1 to n.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "[0, 1, 1, 2, 1]\n", + "5\n" + ] + } + ], + "source": [ + "def countBits(num):\n", + " result=list([0]*(num+1))\n", + " offset = 1\n", + " for i in range(1,num+1):\n", + " if (offset * 2 == i):\n", + " offset *= 2\n", + " result[i] = result[i - offset] + 1;\n", + " print(result)\n", + " return sum(result)\n", + "\n", + "n=int(input())\n", + "print(countBits(n))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}