Monday, October 31, 2011

Solution to ProjectEuler Problem2 - Fibonacci Series till 4 million and sum of even numbers

projecteuler.net problem 2

Problem 2: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Answer: 4613732

Solution:

#!/usr/bin/perl -w
use strict;
my ($sum, $first, $second, $evensum);
$first=1;
$second=2;
$evensum=2;
print "Fibonacci sequence \n";
print "$first $second";
fib($first,$second);


sub fib
{
        my ($num1, $num2)=@_;
        #Limiting fibonacce sequence to 4000000
        if ($num2 <= 3500000 )
        {
                $sum=$num1+$num2;
                print " $sum";
                if ( ($sum%2) == 0 ) {
                        $evensum+=$sum;
                }
                fib($num2, $sum);
        }else {
                print "\nEven Sum = $evensum \n";
                exit (0);
        }
}


Output:
Fibonacci sequence till 4000000
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578
Even Sum = 4613732

Solution to Project Euler Problem1 - sum of all the multiples of 3 or 5 below 1000

Just now I got to know about the site projecteuler.net, which post lots of mathematical problems, that needs computer programming to solve it. As soon I read 2 or 3 problems, I got tempted solve it.
I thought it's giving life back to my dying perl programming skills.

Here is the first problem (they have put easier one to  attract average minds like mine)


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

Answer:   233168

Solution in Perl: 

#!/usr/bin/perl -w
use strict;

my ($i, $div3, $div5, $sum);
$sum=0;
for ($i = 1; $i<1000; $i++)
{
        $div3= $i%3;
        $div5= $i%5;
        if ( ($div3 == 0) || ($div5 == 0) ) {
                $sum+=$i;
                print "$i \n";
        }
}

print "Sum= $sum \n";

Output: 

Multiples of 3 and 5 below 1000 are
 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100 102 105 108 110 111 114 115 117 120 123 125 126 129 130 132 135 138 140 141 144 145 147 150 153 155 156 159 160 162 165 168 170 171 174 175 177 180 183 185 186 189 190 192 195 198 200 201 204 205 207 210 213 215 216 219 220 222 225 228 230 231 234 235 237 240 243 245 246 249 250 252 255 258 260 261 264 265 267 270 273 275 276 279 280 282 285 288 290 291 294 295 297 300 303 305 306 309 310 312 315 318 320 321 324 325 327 330 333 335 336 339 340 342 345 348 350 351 354 355 357 360 363 365 366 369 370 372 375 378 380 381 384 385 387 390 393 395 396 399 400 402 405 408 410 411 414 415 417 420 423 425 426 429 430 432 435 438 440 441 444 445 447 450 453 455 456 459 460 462 465 468 470 471 474 475 477 480 483 485 486 489 490 492 495 498 500 501 504 505 507 510 513 515 516 519 520 522 525 528 530 531 534 535 537 540 543 545 546 549 550 552 555 558 560 561 564 565 567 570 573 575 576 579 580 582 585 588 590 591 594 595 597 600 603 605 606 609 610 612 615 618 620 621 624 625 627 630 633 635 636 639 640 642 645 648 650 651 654 655 657 660 663 665 666 669 670 672 675 678 680 681 684 685 687 690 693 695 696 699 700 702 705 708 710 711 714 715 717 720 723 725 726 729 730 732 735 738 740 741 744 745 747 750 753 755 756 759 760 762 765 768 770 771 774 775 777 780 783 785 786 789 790 792 795 798 800 801 804 805 807 810 813 815 816 819 820 822 825 828 830 831 834 835 837 840 843 845 846 849 850 852 855 858 860 861 864 865 867 870 873 875 876 879 880 882 885 888 890 891 894 895 897 900 903 905 906 909 910 912 915 918 920 921 924 925 927 930 933 935 936 939 940 942 945 948 950 951 954 955 957 960 963 965 966 969 970 972 975 978 980 981 984 985 987 990 993 995 996 999
Sum= 233168

Wednesday, October 19, 2011

Learning the use of "p4 duplicate" command

Another way is to deep rename it:

p4 duplicate //depot/bad_branch_name/... //depot/correct_branch_name/...

Now at that point take a look at the revision graph of some of the files. You'll see that even the integration history is duplicated. This is a command that effectively changes the past.
then:

p4 obliterate (-y) //depot/bad_branch_name/...

However, depending on where the librarian files actually are (check using p4 fstat -Oc //depot/bad_branch_name/... and look for the lbrFile value) you may want to relocate them to the renamed branch before obliterating if they are not lazy copies. Using p4 snap, so for example p4 snap //depot/bad_branch_name/... //depot/correct_branch_name/...

How to create perforce user non-interactively

I was trying to create perforce user account using “p4 user -i” option. I wanted to bulk rename perforce login-ID's due to change in our organization policy.

Here is the command.
p4 user -i -f < tempfile.txt

Where typically a tempfile.txt will be of the form
----------------------------------------------------------------------------------------------
# cat user-template.txt
# A Perforce User Specification.
#
#  User:        The user's user name.
#  Type:        Either 'service' or 'standard'. Default: 'standard'. Read only.
#  Email:       The user's email address; for email review.
#  Update:      The date this specification was last modified.
#  Access:      The date this user was last active.  Read only.
#  FullName:    The user's real name.
#  JobView:     Selects jobs for inclusion during changelist creation.
#  Password:    If set, user must have matching $P4PASSWD on client.
#  Reviews:     Listing of depot files to be reviewed by user.

User:   guruss1

Email:  siddesh.gurusiddappa@emc.com

Update: 2011/10/18 05:04:00

Access: 2011/10/18 06:17:32

FullName:       siddesh

----------------------------------------------------------------------------------------------



In context of the perl script:
open (WRITE, ">", "tempfile.txt") or die "Can't open temp file for writing.\n";
print WRITE "User: $uName\n\n";
print WRITE "Email: $email\n\n";
print WRITE "Update: $date\n\n";
print WRITE "FullName: $fName $lName - $prefix\n\n";
close WRITE;
system( "p4 user -i -f < tempfile.txt");