Wednesday 11 June 2014

Fastest Prime Generator Using the Sieve of Eratosthenes

This code is in python. this takes more time to display numbers than computing:)

thanks to fischer



import sys
import array

def sieve(n):
    sieveBits = (n-1) // 2
    sieveInts = (sieveBits+31) // 32
    sieveBound = int(n**0.5) // 2
    arr = array.array('I')
    arr.extend((0,)*sieveInts)
    for i in xrange(sieveBound):
        if (arr[i >> 5] & (1 << (i&31))) == 0:
            for j in xrange(2*i*(i+3)+3,sieveBits,2*i+3):
                arr[j >> 5] |= 1 << (j&31)
    return arr


def primes(n):
    arr = sieve(n)
    primes = [2] + [2*i+3 for i in xrange((n-1)//2) if arr[i >> 5] & (1 << (i & 31)) == 0]
    return primes

if __name__ == "__main__":
    if len(sys.argv) > 1:
        up = int(sys.argv[1])
    else:
        up = 10000000
    print len(primes(up))

Tuesday 29 April 2014

Working With Signals.


Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
Django provides a set of built-in signals that let user code get notified by Django itself of certain actions. These include some useful notifications:

In this i am showing u step by step process for sucessful 


First step :

Create an Signals.py  file in the folder where u wan to add the signals to models

second step;

There will be an init.py file in  every folder of django .open it and type

import signals

third step open ur signals.py and add the following


# in models.py
from django.db.models.signals import post_save


from django.dispatch import receiver


// below example shows signal that is triggered after saving of model

@receiver(post_save, sender=< ur model name>)
def <function name>(sender, **kwargs):
    # the object which is saved can be accessed via kwargs 'instance' key.
    obj = kwargs['instance']
    print obj.username
    



Excellent On-line Interpreter with 15 Programing Languages


Today, I am going to tell about an online resource where we can practice more than 15 langauages .

Repl.it, is an open source ,online interpreter for many languagues .

it supports
  • JavaScript Variants
    • JavaScript
    • CoffeeScript
    • Kaffeine
    • Move
    • JavaScript.next
  • Esoteric
    • Bloop
    • Brainfuck
    • LOLCODE
    • Unlambda
    • Emoticon
  • Classic
    • Quick Basic
    • Forth
  • Serious
    • Scheme
    • Lua
    • Python
    • Ruby (beta)

Getting the Code

git clone git://github.com/replit/repl.it.git
cd repl.it
git submodule update --init --recursive

Dependencies

node.js

npm

curl https://npmjs.org/install.sh | sh

CoffeeScript

Using npm:
npm install -g coffee-script

Pygments

Using easy_install:
easy_install Pygments
Using pip:
pip install Pygments

Running repl.it

repl.it comes bundled with a static node HTTP file server and a CoffeeScript file watcher & (re)-compiler:
./server.js 8888
repl.it can then be opened at http://localhost:8888/index.html.
Usages:
For example in  python  :http://repl.it/SEB/1




Monday 28 April 2014

The difference in TRUNCATE and DELETE

A common misconception is that they do the same thing.  Not
so.  In fact, there are many differences between the two.
DELETE is a logged operation on a per row basis.  This means
that the deletion of each row gets logged and physically deleted.
You can DELETE any row that will not violate a constraint, while leaving the foreign key or any other contraint in place.
TRUNCATE is also a logged operation, but in a different way.
TRUNCATE logs the deallocation of the data pages in which the data
exists.  The deallocation of data pages means that your data
rows still actually exist in the data pages, but the
extents have been marked as empty for reuse.  This is what
makes TRUNCATE a faster operation to perform over DELETE.
You cannot TRUNCATE a table that has any foreign key
constraints.  You will have to remove the contraints, TRUNCATE the
table, and reapply the contraints.
TRUNCATE will reset any identity columns to the default seed
value.  This means if you have a table with an identity column and
you have 264 rows with a seed value of 1, your last record will have
the value 264 (assuming you started with value 1) in its identity
columns.  After TRUNCATEing your table, when you insert a new
record into the empty table, the identity column will have a value of
1.  DELETE will not do this.  In the same scenario, if you
DELETEd your rows, when inserting a new row into the empty table, the
identity column will have a value of 265.