Jump to content

[PASCAL] Check for Amicable Pair


CouldnoT
 Share

Recommended Posts

Check for Amicable Pair

 

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself.

 

The logic is very simple. We compare sum of the proper divisors of both numbers and compare sum for one number with other number.

 

program Amicable;
uses wincrt;
var
	i,j : Integer;
	
// Function to calculate sum of all  
// proper divisors of a given number 
function SomDiv(nomb: Integer) : Integer;
	var
		sd, i: Integer;	
	begin
		sd:= 0;
		for i:= 1 to nomb do 
			if nomb mod i = 0 then
				sd:= sd+i;
		SomDiv:= sd;		
	end;

// here we try every possible numbers between 1 and 100
// I used j <- i + 1 as we want to try only diffrent numbers      
begin
	for i:= 1 to 100 do
		for j:= i+1 to 100 do 
			if SomDiv(i) = SomDiv(j) then
				writeln(i,' and ', j, ' are amicable.');
	readkey;			
end.

 

 

Edited by CouldnoT
Link to comment
Share on other sites

  • CouldnoT changed the title to [PASCAL] Check for Amicable Pair
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.