How can the year 2011 be expressed as the sum of consecutive primes in MATLAB 7.11 (R2010b) ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I heard that the number 2011 can be expressed as the sum of 11 consecutive prime numbers. How can I find those numbers in MATLAB?
0 Kommentare
Antworten (4)
Walter Roberson
am 20 Jan. 2011
t = 1:2011;t = t(isprime(t)); p = cumsum(t);t(find(p(12:end) - p(1:end-11) == 2011) + (1:11))
0 Kommentare
Matt Fig
am 20 Jan. 2011
Another method:
N = 2011; % Given
P = primes(N/4); % Conservative estimate for ceiling
M = N/11; % The mean
[I,I] = min(abs(filter(ones(1,11)/11,1,P)-M)); % Moving average comparison.
SOL = P(I-10:I) % The desired primes.
sum(SOL)==N %Check that the solution is good
0 Kommentare
John D'Errico
am 20 Jan. 2011
There are many solutions to this. Brute force is one.
>> p = primes(2011);
>> find(conv(p,ones(1,2)) == 2011)
ans =
306
>> find(conv(p,ones(1,3)) == 2011)
ans =
123 307
>> find(conv(p,ones(1,4)) == 2011)
ans =
308
>> find(conv(p,ones(1,5)) == 2011)
ans =
309
>> find(conv(p,ones(1,6)) == 2011)
ans =
310
>> find(conv(p,ones(1,7)) == 2011)
ans =
311
>> find(conv(p,ones(1,8)) == 2011)
ans =
312
>> find(conv(p,ones(1,9)) == 2011)
ans =
313
>> find(conv(p,ones(1,10)) == 2011)
ans =
314
>> find(conv(p,ones(1,11)) == 2011)
ans =
47 315
See that there are two cases where find found more than 1 result that yielded 2011. They correspond to the case for a sum of 3 consecutive primes, and 11 consecutive primes.
>> p(123 + [-2:0])
ans =
661 673 677
>> sum(ans)
ans =
2011
Here is the set of 11 primes.
>> p(47 + [-10:0])
ans =
157 163 167 173 179 181 191 193 197 199 211
>> sum(ans)
ans =
2011
However, you can be more creative. If a set of 11 primes will sum to 2011, then they must average...
>> 2011/11
ans =
182.82
Therefore the middle prime, if a set of 11 consecutive primes will solve this problem, must be roughly 183. The closest prime to 183 is 181. Testing the set of 5 primes below that value and the 5 above it does yield 2011, as we showed above. We can use similar logic to find a set of 17 consecutive primes that sum to the number 17717.
>> 17717/17
ans =
1042.2
Checking the 17 primes that bracket 1042 or so, we find this set, which does sum to the value I specified.
>> sum([991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093])
ans =
17717
6 Kommentare
John D'Errico
am 20 Jan. 2011
Are you sure?
>> 449+457+461
ans =
1367
>> sum([101 103 107 109 113 127 131 137 139 149 151])
ans =
1367
I found 240 as the smallest number with 3 distinct sums, then 311 as the smallest representable as 4 distinct sums of primes.
>> sum([101 103 107])
ans =
311
>> sum([53 59 61 67 71])
ans =
311
>> sum([31 37 41 43 47 53 59])
ans =
311
>> sum([11 13 17 19 23 29 31 37 41 43 47])
ans =
311
Matt Fig
am 20 Jan. 2011
A yes. I was only considering primes, as 2011 is prime. I copied the results for the second prime who has 3 unique sums by mistake. Here are the numbers for 1151:
[379 383 389]
[223 227 229 233 239]
[7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101]
I guess I misunderstood what you were after.
Doug Hull
am 17 Jan. 2011
stack = [];
for i = 1:2011
if isprime(i)
stack = [i stack];
end
if numel(stack) > 11
stack = stack(1:11);
end
if sum(stack) == 2011
disp(sum(stack))
disp(stack)
break
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Linear Algebra finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!