Project Euler Problem 2, solution internals related using ruby debugger

$ruby -rdebug euler-two.rb 
Debug.rb
Emacs support available.

/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57:        RUBYGEMS_ACTIVATION_MONITOR.enter
(rdb:1) r
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57:        RUBYGEMS_ACTIVATION_MONITOR.enter
(rdb:1) 
r
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:57:        RUBYGEMS_ACTIVATION_MONITOR.enter
(rdb:1) next
/usr/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:143:    RUBYGEMS_ACTIVATION_MONITOR.exit
(rdb:1) next
euler-two.rb:3:n1, n2 = 1, 2
(rdb:1) next
euler-two.rb:4:sum = 0
(rdb:1) print n1
1nil
(rdb:1) print n2
2nil
(rdb:1) next
euler-two.rb:6:while n2 < 4000000
(rdb:1) print n2
2nil
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) print sum
0nil
(rdb:1) next
euler-two.rb:8:n1, n2 = n2, (n1 + n2) # iterate through fibonacci seq
(rdb:1) print sum
2nil
(rdb:1) next
euler-two.rb:8:n1, n2 = n2, (n1 + n2) # iterate through fibonacci seq
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) next
euler-two.rb:7:sum += (n2 % 2 == 0) ? n2 : 0
(rdb:1) print sum
2nil
(rdb:1) next
euler-two.rb:8:n1, n2 = n2, (n1 + n2) # iterate through fibonacci seq
(rdb:1) print sum
2nil
(rdb:1) continue
/usr/lib/ruby/2.1.0/debug.rb:290:in `eval':undefined local variable or method `continue' for main:Object
	from /usr/lib/ruby/2.1.0/debug.rb:290:in `rescue in debug_eval'
	from /usr/lib/ruby/2.1.0/debug.rb:287:in `debug_eval'
	from /usr/lib/ruby/2.1.0/debug.rb:661:in `block in debug_command'
	from /usr/lib/ruby/2.1.0/debug.rb:400:in `catch'
	from /usr/lib/ruby/2.1.0/debug.rb:400:in `debug_command'
	from /usr/lib/ruby/2.1.0/debug.rb:859:in `trace_func'
	from /usr/lib/ruby/2.1.0/debug.rb:1085:in `block in '
	from euler-two.rb:8:in `'
(rdb:1) puts

nil
(rdb:1) puts sum
2
nil
(rdb:1) puts n1
2
nil
(rdb:1) puts n2
3
nil
(rdb:1) quit
Really quit? (y/n) y
$

Project Euler Problem 3, solution internals using python debugger

$python -m pdb euler-three.py 
> /home/jeffrin/beautifulwork/lib/euler-three.py(7)()
-> '''
(Pdb) r
6857
--Return--
> /home/jeffrin/beautifulwork/lib/euler-three.py(17)()->None
-> print n
(Pdb) next
--Return--
> (1)()->None
(Pdb) next
The program finished and will be restarted
> /home/jeffrin/beautifulwork/lib/euler-three.py(7)()
-> '''
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(9)()
-> n = 600851475143
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(11)()
-> i = 2
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(12)()
-> while i * i  /home/jeffrin/beautifulwork/lib/euler-three.py(13)()
-> while n % i == 0:
(Pdb) print n
600851475143
(Pdb) print n % i
1
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(15)()
-> i = i + 1
(Pdb) print i
2
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(12)()
-> while i * i  /home/jeffrin/beautifulwork/lib/euler-three.py(13)()
-> while n % i == 0:
(Pdb) print n % i
2
(Pdb) next
> /home/jeffrin/beautifulwork/lib/euler-three.py(15)()
-> i = i + 1
(Pdb) continue
6857
The program finished and will be restarted
> /home/jeffrin/beautifulwork/lib/euler-three.py(7)()
-> '''
(Pdb) 

Project Euler Problem 2, solution internals using python debugger

$python -m pdb project-euler-2.py 
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(13)()
-> '''
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(15)()
-> cache = {}
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(16)()
-> def fib(n):
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(20)()
-> n = 0
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(21)()
-> i = 0
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(22)()
-> while fib(i)  /home/jeffrin/beautifulwork/lib/project-euler-2.py(23)()
-> if not fib(i) % 2: n = n + fib(i)
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(24)()
-> i = i + 1
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(22)()
-> while fib(i)  /home/jeffrin/beautifulwork/lib/project-euler-2.py(23)()
-> if not fib(i) % 2: n = n + fib(i)
(Pdb) next
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(24)()
-> i = i + 1
(Pdb) continue
4613732
The program finished and will be restarted
> /home/jeffrin/beautifulwork/lib/project-euler-2.py(13)()
-> '''
(Pdb) 

http://www.bytemuse.com/post/fibonacci-spiral/

GDB backtrace For chromium hangup

(gdb) bt
#0  pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1  0x00007fb512be9443 in ?? ()
#2  0x00007fb512bc9d0d in ?? ()
#3  0x00007fb512bacb0f in ?? ()
#4  0x00007fb512bbc7d1 in ?? ()
#5  0x00007fb512baa03a in ?? ()
#6  0x00007fb51537aea8 in ?? ()
#7  0x00007fb512b7d045 in ?? ()
#8  0x00007fb512b7de1e in ?? ()
#9  0x00007fb512b7cc30 in ?? ()
#10 0x00007fb512569698 in ChromeMain ()
#11 0x00007fb50a541b45 in __libc_start_main (main=0x7fb512569650 , argc=4, argv=0x7fffb310c918, 
    init=, fini=, rtld_fini=, stack_end=0x7fffb310c908) at libc-start.c:287
#12 0x00007fb512569545 in _start ()

How to write a program to multiply a number by 4 using a bitwise operator ?

/* Part of this program copied from http://www.sanfoundry.com/c-program-multiply-number-4-using-bitwise-operators/ */

#include <stdio.h>

multbitwise()
{

long number;
printf("Enter an integer: ");
scanf("%ld",&number);
number = number << 2;
printf("Result is %ld \n",number);
}

/* http://stackoverflow.com/questions/4456442/interview-multiplication-of-2-integers-using-bitwise-operators */
/* http://www.geeksforgeeks.org/multiply-two-numbers-without-using-multiply-division-bitwise-operators-and-no-loops/ */
/* http://en.wikipedia.org/wiki/Bitwise_operation */

Hacking with conversions of decimal and binary

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
Finding a way for user authentication [24]: 
Finding the binary equivalent of a decimal number and count no. of 1's in binary number [25] 
Option: 25
Enter a decimal integer 
25
 Input number is 25
 It's binary equivalent is 11001 
No. of 1s in the binary number is 3
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
Finding a way for user authentication [24]: 
Finding the binary equivalent of a decimal number and count no. of 1's in binary number [25] 
Option: 22
Enter a binary number : 11001
The binary number is : 11001 
It's decimal equivalent is:  25 
$

How to write an algorithm to reverse an integer ?

/* Part of this work is copied from http://www.sanfoundry.com/c-program-to-reverse-a-given-number/ */

#include <stdio.h>

revint()
{

long num, reverse = 0, temp, remainder;
printf("Enter the number \n");
scanf("%ld",&num);
temp = num;
while ( num > 0 )
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number = %ld \n", temp);
printf("It's reverse = %ld \n", reverse);
}

/* http://stackoverflow.com/questions/15349723/reversing-an-integer-in-java-using-a-for-loop */
/* http://math.stackexchange.com/questions/480068/how-to-reverse-digits-of-an-integer-mathematically */

 

Hacking with an algorithm to reverse an integer

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
23
Enter the number 
47124907242424245782424578
Given number = 9223372036854775807 
It's reverse = 7085774586302733229 
$