考虑形如n/d的分数,其中n和d均为正整数。如果n < d且其最大公约数为1,则该分数称为最简真分数。
如果我们将d ≤ 8的最简真分数构成的集合按大小升序列出,我们得到:
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
可以看出2/5是3/7直接左邻的分数。
将所有d ≤ 1,000,000的最简真分数按大小升序排列,求此时3/7直接左邻的分数的分子。
#include
#include
using namespace std;
const double limit=1000000;
double delt=1;
int a,b;
int gcd(int a,int b)
{
if(!b) return a;
return gcd(b,a%b);
}
int main()
{
for(int i=10;i<=limit;i+=1)
for(int j=(i*3.0/7.0)-3,k=3;k;k--,j+=1)
if(gcd(i,j)==1&&i*3>j*7&&(3.0/7.0)-j*1.0/double(i)
考虑形如n/d的分数,其中n和d均为正整数。如果n < d且其最大公约数为1,则该分数称为最简真分数。
如果我们将d ≤ 8的最简真分数构成的集合按大小升序列出,我们得到:
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8
可以看出该集合中共有21个元素。
d ≤ 1,000,000的最简真分数构成的集合中共有多少个元素?
def euler(n):
ans=n
i=2
while i*i <=n:
if n%i==0:
ans=ans*(i-1)/i
while n%i==0:
n/=i
i+=1
if n<>1:
ans=ans*(n-1)/n
return ans
print sum([euler(x) for x in xrange(2,1000001)])
#303963552391L
#include
using namespace std;
int gcd(int a,int b)
{
if(!b) return a;
return gcd(b,a%b);
}
int main()
{
const int limit=12000;
int ans=0;
for(int i=4;i<=limit;i++)
for(int j=i/3;j<=(i>>1);j++)
if(3*j>i&&2*j
#include
#include
using namespace std;
int fac[]={1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
set st;
bool have[10000001]={0};
int data[100],num=0,sum,cnt;
/***int check(int n)
{
st.clear();
int sum=0;
int cnt=0;
while(st.size() >=cnt&&cnt<60)
{
st.insert(n);
while(n)
{
sum+=fac[n%10];
n/=10;
}
n=sum;
sum=0;
cnt++;
}
return cnt;
}***/
int check(int n)
{
num=sum=cnt=0;
while(!have[n])
{
have[n]=true;
data[num++]=n;
while(n)
{
sum+=fac[n%10];
n/=10;
}
n=sum;
sum=0;
cnt++;
}
for(int i=0;i=60)
ans++;
cout<= 60:
return 1
else:
return 0
ans=0
for x in xrange(1,1000000):
ans+=cal(x)
***/
#include
using namespace std;
const int limit=1000001;
int have[10000006]= {0};
int fac[]= {1,1,2,6,24,120,720,5040,40320,362880};
int sum,cnt,tmp[100];
int cal(int n)
{
if(have[n]>0) return have[n];
cnt=0;
while(!have[n])
{
have[n]=-1;
tmp[cnt++]=n;
sum=0;
while(n)
{
sum+=fac[n%10];
n/=10;
}
n=sum;
}
have[tmp[0]]=have[n]==-1?cnt:cnt+have[n];
for(int i=1; i=60)
ans++;
cout<
#include
#include
#include
using namespace std;
const int limit=1500000;
int gcd(int a,int b)
{
return !b?a:gcd(b,a%b);
}
int Len[limit+5]= {0};
int main()
{
int a,b,c,ans=0,sum;
for(int m=1; m<=1224; m++)
for(int n=1; nb) swap(a,b);
if(sum<=limit&&gcd(c,gcd(b,a))==1)
{
for(int s=sum; s<=limit; s+=sum)
Len[s]++;
}
}
for(int i=1; i<=limit; i++)
if(Len[i]==1) ans++;
cout<
#include
#include
#include
using namespace std;
const int cnt=101;
long long mul[cnt]={0};
long long tmp[cnt]={0};
int fact[cnt];
int main()
{
mul[0]=1;
for(int i=0;i=cnt)
break;
else
tmp[k+j]+=mul[k];
}
copy(tmp,tmp+cnt,mul);
}
cout<
#include
#include
#include
using namespace std;
int cnt=0;
const int limit=100;
int mul[limit+5]={0};
int tmp[limit+5]={0};
#define mod 1000000
int prime[limit+5];
bool isprime(int n)
{
for(int i=2;i*i<=n;i++)
if(n%i==0) return false;
return true;
}
int main()
{
mul[0]=1;
cnt=0;
for(int i=2;ilimit)
break;
else
{
tmp[k+j]+=mul[k];
//tmp[k+j]%=mod;
}
}
copy(tmp,tmp+limit,mul);
}
for(int i=2;i=5000)
{
cout<
#include
#include
#include
#include
#include
#define mod 1000000
using namespace std;
//product function
const int limit=60000;///55374
int mul[limit+5]= {0}, tmp[limit+5]= {0},cnt=0;
int main()
{
int start=clock();
freopen("outp78.txt","w",stdout);
for(int i=0; i<=limit; i++)
tmp[i]=mul[i]=1;
for(int i=2; ilimit)
break;
else
{
tmp[k+j]+=mul[k];
tmp[k+j]%=mod;
}
copy(tmp,tmp+limit,mul);
if(mul[i]%mod==0)
{
cout<
# list of generalized pentagonal numbers for generating function
k = sum([[i*(3*i - 1)/2, i*(3*i - 1)/2 + i] for i in range(1, 250)], [])
p, sgn, n, m = [1], [1, 1, -1, -1], 0, 1e6
while p[n]>0: # expand generating function to calculate p(n)
n += 1
px, i = 0, 0
while k[i] <= n:
px += p[n - k[i]] * sgn[i%4]
i += 1
p.append(px % m)
print "Project Euler 78 Solution =", n
import time
start = time.time()
f = open('p079_keylog.txt', 'r') #opens the file
l = [] #initiates a list for the entries
for line in f: #create the list and remove newline characters
l.append(line.rstrip('\n'))
a = {} #initiate a dictionary - will be a dictionary of sets for each integer
k = 0
while k < 10: #loop from 0-9 excluding 4 and 5
if k == 4: k = 6
s1 = set()
key = k
for passTry in l: #for each password attempt list every number that comes after another
for num in passTry:
if passTry[0] == str(k):
s1.add(passTry[1]) #add to sets
s1.add(passTry[2])
if passTry[1] == str(k):
s1.add(passTry[2])
a[key] = s1
k += 1
#sort by length of set
for k in sorted(a, key=lambda k: len(a[k]), reverse=True):
print k,
print "\nExecuted in " + str((time.time()-start)*1000) + " ms"
def cal(n):
ans=int(n**0.5)
for x in range(100):
n*=100
tmp=ans*10
while tmp*tmp<=n:
if tmp*tmp==n:
return 0
tmp+=1
ans=tmp-1
return sum([int(x) for x in str(ans)[0:100:]])
print sum([cal(x) for x in range(100)])#40886