Question:
Find the smallest number that can be expressed as
the sum of 3 consecutive prime numbers,
the sum of 11 consecutive prime numbers,
the sum of 25 consecutive prime numbers,
the sum of 171 consecutive prime numbers,
the sum of 1225 consecutive prime numbers,
and is itself a prime number.
For example, 41 is the smallest prime number that can be expressed as
the sum of 3 consecutive primes (11 + 13 + 17 = 41) and
the sum of 6 consecutive primes (2 + 3 + 5 + 7 + 11 + 13 = 41).
Your answer:
My solution with VB6
- Dima(100000000)AsByte,p(10000000)AsLong,numAsLong,nAsLong
- SubGetprimes()
- Dimi&,j&,k
- p(0)=2'The1stprime
- k=10000'sqrarerootof10^8
- n=100000000'10^8
- Fori=3TokStep2
- Ifa(i)=0Then
- num=num+1
- p(num)=i
- Forj=i*iTonStep2*i'Eractosthenes
- a(j)=100'Notprimenumber
- Next
- EndIf
- Next
- Fori=k+1TonStep2'Listallprimenumberstoarrayp()
- Ifa(i)=0Then
- num=num+1
- p(num)=i
- EndIf
- Next
- EndSub
- PrivateSubCommand1_Click()
- DimsAsString,tmAsSingle
- s=InputBox("Pleaseenternumbers:","Info","3,11,25,171,1225")'Inputallnumbersthequestionhaslisted
- tm=Timer
- Getprimes
- s=minprime(s)
- tm=Timer-tm
- Clipboard.Clear
- Clipboard.SetTextCStr(s)'CopytheanswertoClipboard
- MsgBox"Itcostmeabout"&Format(tm,"0.0000")&"secondstofindtheanswer:"&s&vbCrLf&"Andithasbeencopiedtotheclipboard"
- EndSub
- Functionminprime(myprimesAsString)AsLong
- Dimi&,j&,sum()AsLong,countAsLong,primedata
- primedata=Split(myprimes,",")
- count=UBound(primedata)
- ReDimsum(count)
- Fori=0Tocount
- Forj=1Toprimedata(i)'Smallsumofcontinuousprimenumbers
- sum(i)=sum(i)+p(j)
- Next
- Ifa(sum(i))<100Thena(sum(i))=a(sum(i))+1'Meetoneoftheconditions
- Forj=primedata(i)+1Tonum
- sum(i)=sum(i)+p(j)-p(j-primedata(i))
- Ifsum(i)>nThenExitFor
- Ifa(sum(i))<100Thena(sum(i))=a(sum(i))+1'Meetoneoftheconditions
- Ifa(sum(i))=count+1Thenminprime=sum(i):ExitFunction'Meetalloftheconditions,Ok
- Next
- Next
- EndFunction
It returns:
6954293