Projecteuler -- 21 (Amicable numbers)

Solution :  If you don't understand the solution process I will recommend you to go my blog's algorithm section and search "Divisor কথন" and read it hope you will understand.


problem Description : Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.



Project Euler -- 48 (Self powers)

Problem : The series, 11 + 22 + 33 + ... + 1010 = 10405071317.
Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.

My solution approach is,

first made a smaller version of the problem then I tried to solve it after solving this I was going to solve the bigger version.

// a. find out p=2^15

// b. print the last digit of p







উইলসন থিওরেম

প্রাইম নাম্বার বের করার জন্য সিভের অ্যালগরিদম প্রায় সবাই জানে, অনেকের অ্যালগোরিদম শেখার শুরুটাও এই সিভ দিয়ে আমার নিজের তাই। যাই হোক প্রাইম নাম্বার চেক করার জন্য আর একটি সহজ থিওরি হচ্ছে এই উইসন থিওরেম।
উইলসন থিওরেমকে এভাবে বলা যায়, একটি natural number  n( n>1 )প্রাইম হবে যদি (( n-1)!) mod n =n-1 হয়।
এটাই হচ্ছে উইলসন থিওরেম
(n-1) ! = 1*2*3*4*5*……………………*n-1 ;

একটা উদাহরণ দেওয়া যাক,

ধরা যাক n=6

(n-1)! mod n = 5! Mod 6 = 0
0 is not equal to n-1 = 5 , So n=6  is not prime .

Another Example :

Assume, n=7
(n-1)! mod n = 6! Mod 7 = 6
6 is equal to n-1 = 6 , So n=7  is prime .



এভাবে একে implement করা যায়,





একটা বিষয় মাথায় রাখতে হবে অনেক বড় সংখ্যার প্রোগ্রামটি ক্ষেত্রে একটু slow কাজ করতে পারে সেক্ষেত্রে আবার mod m অর্থাৎ fact=((fact*i) %m)%m;  করতে হবে।  

উইলসন থিওরেম এর প্রমাণ এই লিঙ্কে  সুন্দর ভাবে দেওয়া আছে,কষ্ট করে দেখে নিবেন।


তবে আমি একটু অন্যভাবে এইটা প্রমাণ করার চেষ্টা করছি, সেটা হচ্ছে সিভের অ্যালগোরিদম দিয়ে 1 থেকে 10000000 পর্যন্ত প্রাইম নাম্বার বের করি এবং একটা prime.txt ফাইলে সেভ করি তারপর উইলসন দিয়ে চেক করি প্রাইম নাকি প্রাইম নয় এবং primeCheck.txt ফাইলে সেভ করি, এরকম ভাবে,  


একটা output দেখা যাক 10000000 পর্যন্ত ঠিকঠাক কাজ করতেছে। 


আপনারা চাইলে এখান থেকে পোস্টটি txt ফাইল সহ  PDF Download করে নিতে পারেন।
লেখাটা বাংলিশ হয়ে গেল আশা করি ক্ষমা সুন্দর দৃষ্টিতে দেখবেন।

** আমার কোথাও ভুল হতে পারে,দয়া করে মন্তব্য করবেন।ভুল করার প্রবনতাই মানুষকে অন্যান্য সৃষ্টি থেকে আলাদা করে। 


Exception Handling in Java

The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

Definition : An Exception represent an error condition that can occur during the normal course of program execution. When an exception occur the normal sequence of flow is terminated and the exception-handling routine is executed. When an exception occurs we say an exception is thrown ,When the matching  exception handling code is executed we say the thrown exception is caught.

In Other word we can say an exception handling is a mechanism to handle to run time error of program such as Arithmetic Error ,IO error etc.

When an exception occurs we say exception is “thrown” and handling exception is called  “caught” the exception.

Definition 2 : In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

Advantage of Exception Handling



The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:

1.      statement 1;  
2.      statement 2;  
3.      statement 3;  
4.      statement 4;  
5.      statement 5//exception occurs  
6.      statement 6;  
7.      statement 7;  
8.      statement 8;  
9.      statement 9;  
10. statement 10;  
1.      statement 1;  
2.      statement 2;  
3.      statement 3;  
4.      statement 4;  
5.      statement 5;//exception occurs  
6.      statement 6;  
7.      statement 7;  
8.      statement 8;  
9.      statement 9;  
10. statement 10;  

Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the exception will be executed. That is why we use exception handling in java.

Types of Exception :
1.      Checked Exception
2.      Unchecked Exception

A checked Exception is an exception that is checked at compile time all other exceptions are unchecked exception also called Runtime exception.
Unchecked exceptions are not checked at compile-time rather they are checked at  Runtime , ArithmeticException, NumberformatException are two example of Runtime exception.

Some Example of Exception :

a.     ArithmethicException :
Int a=13/0;

b.     NumberFormatException :





** Understanding parseInt() method:

Instead of using the data type specific methods such as nextInt, nextDouble,
and others of the Scanner class, we can input a numerical value in a string format
and convert it to an appropriate data type by ourselves. For example, we can use the class method parseInt of the Integer class to convert a string to an int. Here’s a statement
In other word parseInt() takes numerical value as an integer and return integer.

that converts "123" to an int value 123:
int num = Integer.parseInt("123");

c.      NullPointerException :

If we have null value in any variable and if we try to perform any operation by the variable then it occurs a NullPointerException.



Getting Started In Exception :

try-catch Block :

try and catch block is used to handle exception. try block is used to enclose the code that might throw an exception. It must be used within the method.
Java catch block is used to handle the Exception. It must be used after the try block only. We can use multiple catch block with a single try.
If an exception occurs inside the try block then the statements remaining inside try block is skipped and the execution flow passed to the matching catch (handler block) block.



Here is a simple figure it might be helpful.


** keyword : throw and throws.

The “throw” keyword is used to throw an exception.
We can throw either checked or unchecked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception.

Syntax of “throw” : throw < a throwable object>
Example : throw new myexception(“This is an Error.”);

As I mentioned before < a throwable object > which is an instance of Exception Class. When create an instance of the Exception class we can pass the String that describes the error, the thrown exception is caught by the corresponding catch block and the error message will be displayed.

Simple Code :

package exceptionhandeling;
import java.util.*;

public class NewClass3 {   
    public static void main(String args[]){       
        Scanner input=new Scanner(System.in);
        int age;          
                println("Enter your Age : ");
        
                try{
                     age=input.nextInt();                
                     if(age<=0){
                         throw new Exception("What the Hell you are Doing !!");
                     }
                     if(age>99)
                         throw new Exception("Amare Ki Mone hoy ?? ");             
                }
                catch(InputMismatchException myexception){
                    input.next();
                   
                    println("Please Enter Digit only.");
                }
                catch(Exception myexception){
                    println("Error : "+ myexception.getMessage());
                }
    } 
    public static void println(Object n){
        System.out.println(n);
    }
}

Enough for today. I will try to write more dipper in Exception Handling to my next article I hope you enjoyed it. May be I made some mistake please let me know about so that I can improve it.

Author : Md. Shohanur Rahaman

Date : 06-02-2015.

Download Pdf