Tuesday 25 August 2015

.>>>>>>>>>>>Counting sort in ruby(PL)
#author "shivamzaz" @imsec 
a=[1,1,3,2]   #take any one arrray
d=a.max
c=Array.new((d+1)) { |i|  i=0 }
for i in 0...a.length
    c[a[i]]+=1
end
for i in 1...c.length
    c[i]=c[i]+c[i-1]
end
b=Array.new(c.max) {|v| v=0}
for i in (a.length-1).downto(0)
    b[c[a[i]]]=a[i]
    c[a[i]]-=1
end
i=a.min
while(b[i]!=nil)
    puts b[i]
    i+=1
end

>>>>>>>>>>>>>>happy coding<<<<<<<<<<<<<<<<<<

Monday 10 August 2015

Number of unique elements using c++ in o(n):
    /*author => shivamzaz
                            level=> ad-hoc
          complexity=> o(n)  */
    #include "bits/stdc++.h"
      using namespace std;
    int main(){
    int a[]={2,2,3,2},cnt=0;
    bool b[4]={0};
    for(int i=0;i<4;i++){
        b[a[i]]=1;
    }
    for(int i=0;i<4;i++){
        if(b[i]==1){
            cnt++;
        }
    }
    printf("%d\n",cnt);
    return 0;
    }

Saturday 8 August 2015

Quickly find fibonacci numbers(using matrix property).
 
 Divide_Conquer_Fib(n) {
       i = h = 1;
       j = k = 0;
      while (n > 0) {
               if (n%2 == 1) { // if n is odd
                   t = j*h;
                    j = i*h + j*k + t;
                    i = i*k + t;
               }
       t = h*h;
       h = 2*k*h + t;
       k = k*k + t;
       n = (int) n/2;
     }
return j;
}

Saturday 25 July 2015

efficiently finding, number is multiple of 3 or not..(in ruby PL)
#author "shivamzaz"
#efficient for nu,ber is multipe of three or not.
#level "medium"
#specially use of bitwise operator
'''Example: 23 (00..10111)
    1) Get count of all set bits at odd positions (For 23 it’s 3).
    2) Get count of all set bits at even positions (For 23 it’s 1).
    3) If difference of above two counts is a multiple of 3 then number is also a multiple of 3.'''
#<---------------------Happy Coding-------------------------< implementation in #ruby>--------------------------Happy Coding---------------------->
g=gets().chomp.to_i
odd=0
even=0
for i in 1.upto(8)
    if(((g & (1<<(i-1)))>0) && (i%2!=0))    #(>0  becoz ruby have'nt if(1) type of stmts while takes if(1>0) means to say conditional stmts..
        odd+=1
        #puts("odd :#{i}")
    elsif(((g & (1<<(i-1)))>0) && (i%2==0))
        even+=1
        #puts("even :#{i}")

    else
        p+=1
    end
end
puts odd
puts even
if((odd-even)%3==0)
    puts("multiple of three ")
else
    puts("not multiples of three")
end

Friday 17 July 2015

Data structures in Ruby for beginners(Linked List)
'''author => shivamzaz  @imsec
    tag => simply linked list creation
    level => easy but not cakewalk
    '''
Node=Struct.new(:value,:next)
#  optional   <=>
'''class Node
    attr_accessor :value , :next        #value by ll and val by user function call
    def initialize value, next_i
        @value = value
        @next = next_i
        #puts "initialized with value :" + value.to_s
    end
end'''
class LinkedList
    def initialize value    # head ( first node creation ) creation
        @head= Node.new(value,nil)   #=> [5,nil] initially
    end
    def add(value)
        current = @head
        while(current.next!=nil)
            current=current.next
        end
        current.next=Node.new(value,nil)
    end
    def delete(value)
        current=@head
        if(current.value==value)
            @head=@head.next
        else
            current=@head
            while((current.next.value!=value)&& (current.next!=value) && (current!=nil))    # previous index tracking..
                current=current.next
            end
            if(current!=nil && current.next!=nil)
                current.next=current.next.next
            end
        end
    end
    def display()
        current=@head
        full_list=[]
        while(current.next!=nil)
            full_list<<current.value.to_s
            current=current.next
        end
        full_list+=[current.value.to_s]
        puts full_list.join("->")
    end
end
ll=LinkedList.new(5)   # first node  creation.
#ll=LinkedList.add(5)
ll.add(5)
ll.add(10)
#ll.delete(10)
ll.display


-------------------------------------------------------------------------------------------------------------------------------
                                      <<<Happy Coding>>>                      ->Shivam Gupta
Efficient  fibonacci series in ruby..

       '''author -> shivamzaz @imsec
    tag -> effiecient fibonacci series
    level -> cakewalk'''
p=gets().to_i
l=[]
l<<0
l<<1
q=gets().to_i    #qth num of fibonacci.
for i in p..1000   #1000 optional means upto series...
    if(((Math.sqrt(5*i*i+4)).floor)==(Math.sqrt((5*i*i+4)).ceil) || (Math.sqrt((5*i*i-4)).floor)==(Math.sqrt((5*i*i-4)).ceil))
        l<<i
    end
end
puts(l[q-1])    #print qth fibonacci num.
'''output:
       1
       3
       1

Thursday 16 July 2015

efficient prime algorithm in ruby(sieve of eratosthenes)....
  '''author -> shivamzaz @imsec
   tag -> effiecient prime finfinding algorithms
   complexity -> O(n loglog n)
   level -> cake walk'''
p=gets().to_i
prm=[]
for i in 0..p
    prm[i]=1
end
prm[0]=0
prm[1]=0
for i in 2..(Math.sqrt(p)).floor
    if(prm[i]==1)
        j=2
        while((i*j)<=p);    # coz table started from j->2
            prm[i*j]=0
            j+=1
        end
    end
end
for i in 0..p
    if(prm[i]==1)
        puts i
    end
end
'''
output:
    15
    2
    3
    5
    7
    11
    13
'''

Friday 8 May 2015

Factorial of large number in ruby(programming language)....
#Author"shivamzaz"@imsec(2cs)(2ndyr)
#Based on simple maths concept(carry based)
#Large value factorial in ruby...
#Session"2015"
def factorial(n);
    a=[]        #intialize array
    a[0]=1      #let fist value of array is 1 as usual like factorial simple defn concept
    a_size=1     
    for x in 2..n;      
        a_size=mult(x,a,a_size)    #function call
    end
    g=a.size-1                     #display the result in reverse wise.....due to carry maintenance
    while(g>=0);
        print a[g]
        g-=1
    end
end
def mult(x,a,a_size);                        
    cry=0
    for i in 0..a_size-1;
        pro=x*a[i]+cry
        cry=pro/10
        a[i]=pro%10
    end
    while(cry>0);
        a[a_size]=cry%10
        cry=cry/10
        a_size+=1
    end
    return a_size
end
factorial(gets().to_i)    #fucnction call
---------------------------------------------------------------------------------------------------------------------------------------------
that's all...

                                <<<<<<<Happy Coding>>>>>.

Tuesday 21 April 2015

Divide two values without using divide or multiply operator using c++ and ruby */
Using c++;
 #include"bits/stdc++.h"
int main(){
    int t,p,y,cnt=0;
    scanf("%d%d%d",&y,&t,&p);
    while(y--){   //t=>dividend p=>divisor
        while(1){
            if(t>=p){ //our need should be follow in this way...process this condition //untill t is lesser than divisor(p)
                cnt+=1;
                t-=p;}
            else{break;}

        }
    printf("%d\n",cnt);}
return 0;}
Using ruby;
#instruction will be follow as above code....
y,t,p=gets().split(" ").map{|m| m.to_i}
while(y>0);
    while(1);
         if(t>=p)
             cnt+=1 
             t-=p
         else
             break
         end
   end
puts cnt
end
Most imp point related to above codes;
1.above code only return floor values.
2.for accurate value use this formula a/b=exponential(log(a)-log(b))
that's all                   

Friday 20 February 2015


array input without loop or goto stmt...
        #include "stdio.h"
        void shivamzaz(int i);
        int n; // globaly declare coz of globaly access.
        //scanf("%d",&n);
int main(){
    int i=0;
     scanf("%d",&n);
    shivamzaz(i);
return 0;
}
void shivamzaz(int i){
    static int a[10000];
    if(i<n){
        scanf("%d",&a[i]);
    shivamzaz(i+1);}
}