My Weblog

Blog about programming and math

Project Euler 87

Simple problem. Just generate all the primes below 10000 and find out all the required numbers using three loops and given condition. Remember of duplicates as it sometimes kills ;).

#include<cstdio>
#include<vector>
#include<iostream>
#include<map>
#define Lim 50000000
using namespace std;
typedef long long ll;
vector<ll> V;
bool f[10000];
 bool _fun()
	{

		f[0]=f[1]=1;
		for(int i=2;i*i<10000;i++)
		 if(!f[i]) for(int j=i*i;j<10000;j+=i) f[j]=1;

		for(ll i=2;i<10000;i++)if(!f[i]) V.push_back(i);
		
		
	 }


int main()
	{

		long long sum=0;
		map<ll,bool> M;
		_fun();
		for(ll i=0;i<V.size() && V[i]*V[i]<Lim; i++)
		 {
			for(int j=0;j<V.size() && (V[i]*V[i]+V[j]*V[j]*V[j])<Lim;j++)
			 {
				for(int k=0;k<V.size() && (V[i]*V[i]+V[j]*V[j]*V[j]+V[k]*V[k]*V[k]*V[k])<Lim;k++)
				{
					if(M[V[i]*V[i]+V[j]*V[j]*V[j]+V[k]*V[k]*V[k]*V[k]]) continue;
					M[V[i]*V[i]+V[j]*V[j]*V[j]+V[k]*V[k]*V[k]*V[k]]=1;
					sum++;
				}
			}
		}

		cout<<sum<<endl;
	}
			
 
Advertisements

March 24, 2010 - Posted by | Programming

1 Comment »

  1. i dont know who you are but thanks a lot. good solution 😀

    Comment by biyro | August 6, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: