Archive

Posts Tagged ‘Algorithms’

Project euler 29: Distinct powers

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

def distpowers(n):
a={2} # initialize the set
d={}  # map for total number of elements for each power
s=[0 for x in range(n + 1)]
for i in range(1, 8):
for j in range(2, n + 1):
d[i]=len(a)
tot = 0
for i in range (2, n + 1):
if s[i] == 0:
prod = i
power = 0
while prod <= n:
s[prod] = 1
prod *= i
power += 1
tot += d[power]
print (tot)

if __name__ == '__main__':
distpowers(100)

Time complexity O(8 * n) ~ O(n)
Space complexity O(n)

Categories: Programming Tags: ,

Project euler: Problem 23 Non Abundant sums

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
#define MAX 28123

vector<long> abundant;

int allabundant(int num)
{
long sum = 0;
long p = 1;
for (long i = 2; i <= num; i++) {
sum = 1;
p = 2;
while (p * p <= i) {
if (i % p == 0) {
sum += p;
if (p != i / p)
sum += (i / p);
}
p++;
}
if (sum > i)
abundant.push_back(i);
}
}

int main()
{
long long sum = 0;
allabundant(MAX);
bool boolabund[MAX] = {false};
cout<< "Total number of abundant numbers below "<<MAX<<": "<<abundant.size()<<endl;
for(long i = 0; i < abundant.size(); i++) {
for(long j = i; j < abundant.size(); j++) {
long long temp = abundant[i] + abundant[j];
if (temp > MAX)
break;
boolabund[temp] = true;
}
}
for (int i = 0; i <= MAX; i++)
if (boolabund[i] == false)
sum += i;
}

time ./a.exe
Total number of abundant numbers below 28123: 6965

real 0m0.193s
user 0m0.124s
sys 0m0.046s

Categories: Programming

Divide and Conquer method Illustrated

Divide and Conquer is one of the famous algorithmic techniques. It works with the philosophy that divide the whole problem into smaller managable chunks of subproblems and work out these small subproblems there by combining the intermediate partial solutions.

There are many famous examples which use the Divide and Conqure strategy, for example Binary search, Merge sort, Insertion sort, Quick sort etc.,

Let us take a small example of finding minimum and maximum elements in a given array. If we adapt a normal methodology it takes atleast 2*(n-1) comparisions. We can decrease the number of comparisions using this technique which results in [ Ceil(3*n/2)-2 ] comparisions.

The below is the code for finding MIN and MAX of the given array which was compiled and tested using GNU g++compiler.

//Author: Vasanth Raja Chittampally
#include <iostream>

using namespace std;

void findMin(int [],int, int,int &amp;, int &amp;);

int main()

{

int n,min,max;

cout<<"Enter n:";

cin>>n;

int a[n];

for(int i=0;i<n;i++)

{

cout<<"Enter a["<<i<<"]:";

cin>>a[i];

}

findMin(a,0,n-1,min,max);

cout<<"Minimun of the given array is: "<<min<<;endl;

cout<<"Maximum of the gvien arrays is: "<<max<<endl;

return 0;

}

void findMin(int a[], int left, int right, int &amp;min, int &amp;max)

{

if(left==right)

{

min=a[left];

max=a[right];

}

else

{

int mid=(left+right)/2;

findMin(a,left,mid,min,max);

int left_min=min;

int left_max=max;

findMin(a,mid+1,right,min,max);

int right_min=min;

int right_max=max;

if(left_min&lt;right_min)

min=left_min;

else

min=right_min;

if(left_max&gt;right_max)

max=left_max;

else

max=right_max;

}

}

In this example it divides whole array into two equal parts recursively and tries to compare partial solutions to find the minimum and maximum elements.

T(n)=2T(n/2) + 2 —–>(2 comparisions are needed one for minimum and the other of maximum)

.

.

.

.

number of comparisions = [ Ceil(3*n/2)-2 ]

The above number of comparisions can be verified by substituting in T(n)

T(n)=2*[3*n/(2*2) – 2]+2

T(n)=3n/2-2 (hence verfied)