The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive
primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to
a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most
consecutive primes?
I'm using Racket (a dialect of scheme) for this example, but this should be language agnostic. In the question, it states that the sum of the first 21 consecutive primes is 953. So, I went to test this out (I had already written my code for this problem and it was working incorrectly).
> (define primes (filter prime? (range 2 10000)))
> (apply + (take primes 6)) ; This is 41: Good so far!
> ; This is where it gets odd.
> (apply + (take primes 21)) ; This is 712. And, after further experimentation, there is amount of summed primes that is 953.
> (apply + (take primes 23)) ; This is 874.
> (apply + (take primes 24)) ; This is 963.
Is there something I'm missing about the question?
Euler #50 asks for sums of consecutive primes, which need not necessarily begin with the first prime. The fact that the shown example starts with the first prime is incidental (although it is no accident that the winning sequence starts with at a small prime).
953 = 7 + 11 + 13 + 17 + 19 + 23 + 29
+ 31 + 37 + 41 + 43 + 47 + 53 + 59
+ 61 + 67 + 71 + 73 + 79 + 83 + 89
That's 21 terms. There is no mistake in the problem description - the term 'first' does not appear anywhere in the text.
You read the question wrong. The sum must also be a prime, which 963 is not (107 * 9, for example).
Related
for a=1:50; %numbers 1 through 50
for b=1:50;
c=sqrt(a^2+b^2);
if c<=50&c(rem(c,1)==0);%if display only if c<=50 and c=c/1 has remainder of 0
pyth=[a,b,c];%pythagorean matrix
disp(pyth)
else c(rem(c,1)~=0);%if remainder doesn't equal to 0, omit output
end
end
end
answer=
3 4 5
4 3 5
5 12 13
6 8 10
7 24 25
8 6 10
8 15 17
9 12 15
9 40 41
10 24 26
12 5 13
12 9 15
12 16 20
12 35 37
14 48 50
15 8 17
15 20 25
15 36 39
16 12 20
16 30 34
18 24 30
20 15 25
20 21 29
21 20 29
21 28 35
24 7 25
24 10 26
24 18 30
24 32 40
27 36 45
28 21 35
30 16 34
30 40 50
32 24 40
35 12 37
36 15 39
36 27 45
40 9 41
40 30 50
48 14 50
This problem involves the Pythagorean theorem but we cannot use the built in function so I had to write one myself. The problem is for example columns 1 & 2 from the first two rows have the same numbers. How do I code it so it only deletes one of the rows if the columns 1 and 2 have the same number combination? I've tried unique function but it doesn't really delete the combinations. I have read about deleting duplicates from previous posts but those have confused me even more. Any help on how to go about this problem will help me immensely!
Thank you
welcome to StackOverflow.
The problem in your code seems to be, that pyth only contains 3 values, [a, b, c]. The unique() funcion used in the next line has no effect in that case, because only one row is contained in pyth. another issue is, that the values idx and out are calculated in each loop cycle. This should be placed after the loops. An example code could look like this:
pyth = zeros(0,3);
for a=1:50
for b=1:50
c = sqrt(a^2 + b^2);
if c<=50 && rem(c,1)==0
abc_sorted = sort([a,b,c]);
pyth = [pyth; abc_sorted];
end
end
end
% do final sorting outside of the loop
[~,idx] = unique(pyth, 'rows', 'stable');
out = pyth(idx,:);
disp(out)
a few other tips for writing MATLAB code:
You do not need to end for or if/else stements with a semicolon
else statements cover any other case not included before, so they do not need a condition.
Some performance reommendations:
Due to the symmetry of a and b (a^2 + b^2 = b^2 + a^2) the b loop could be constrained to for b=1:a, which would roughly save you half of the loop cycles.
if you use && for contencation of scalar values, the second part is not evaluated, if the first part already fails (source).
Regards,
Chris
You can also linearize your algorithm (but we're still using bruteforce):
[X,Y] = meshgrid(1:50,1:50); %generate all the combination
C = (X(:).^2+Y(:).^2).^0.5; %sums of two square for every combination
ind = find(rem(C,1)==0 & C<=50); %get the index
res = unique([sort([X(ind),Y(ind)],2),C(ind)],'rows'); %check for uniqueness
Now you could really optimized your algorithm using math, you should read this question. It will be useful if n>>50.
This question already has answers here:
Implementing matching pursuit algorithm
(3 answers)
Closed 6 years ago.
I'm trying to implement Matching Pursuit algorithm in Matlab.I have found out the maximum inner product value ,i m stuck with how to find out the coeffients.
help me out.
Here is the algorithm
D=[1 6 11 16 21 26 31 36 41 46
2 7 12 17 22 27 32 37 42 47
3 8 13 18 23 28 33 38 43 48
4 9 14 19 24 29 34 39 44 49
5 10 15 20 25 30 35 40 45 50];
b=[16;17;18;19;20];
n=size(D);
A1=zeros(n);
R=b;
x=[];
H=10;
if(H <= 0)
error('The number of iterations needs to be greater then 0')
end;
[c,d] = max(abs(D'*R));
Here i have used a prefined dictionary.
Thanks in advance
You can use this function based on "S. Mallat, Z. Zhang, 1993. Matching pursuit in a time frequency dictionary. IEEE Transactions Signal Processing, Vol. 41, No. 12, pp. 3397-3415."
x = MP(b,D,10);
function S = MP(y,Dictionary,iteration)
n = size(Dictionary,2);
S = zeros(n,1);
% Normalize the dictionary atoms (coloumns) to have unit norm
% It's better to implement this part out of function,
% to normalize the dictionary just one time!
%**************************************
for j = 1:n;
Dictionary(:,j) = Dictionary(:,j)/norm(Dictionary(:,j));
end
% *************************************
for i = 1:iteration
gn = Dictionary' * y / norm(y);
[MAX,index] = max(abs(gn));
y = y - MAX * Dictionary(:,index);
S(index) = MAX + S(index);
end
So I'm reading a book on how binary bits are converted into octal numbers.
When trying to explain the concept, they give this equation
N= S(...((d8)2^8+(d7)2^7+(d6)2^6)+((d5)2^5+(d4)2^4+(d3)2^3)+((d2)2^2+(d1)2^1+d0))
or
N= S(...((d8)2^2 +(d7)2+(d6))2^6 + ((d5)2^2 +(d4)2^1 + (d3))2^3 + ((d2)2^2+(d1)2^1+d0))
d represents the digit found within the bit, e.g. if the least significant bit was 1, then (d0) would be 1.
I understand all of this, but they elaborate further saying that the parenthesized expressions ((d8)2^2 +(d7)2+(d6)) are coefficients of base 8 digits, N=S((d2)8^2+(d1)*8+(d0)).
Can someone explain what they mean by the parenthesized expressions being coefficients of base8 digits?
The digits di are the binary digits of the number. We can compute the number from its binary digits like this:
n = ∑ i 2i di = 20 d0 + 21 d1 + 22 d2 + ⋯
(This is in fact what defines “binary”, if we add the condition that the digits are integers and 0 ≤ di < 2 for all i.)
Suppose we name the octal digits of the number oj. We can compute the number from its octal digits like this:
n = ∑ j 8j oj = 80 o0 + 81 o1 + 82 o2 + ⋯
(This is what defines “octal”, if we add the condition that the digits are integers and 0 ≤ oj < 8 for all j.)
Now let's look back at the binary equation. The first step is the trickiest. We will change the way the subscript is used so that each term of the summation uses three binary digits:
n = ∑ j 23 j + 0 d3 j + 0 + 23 j + 1 d3 j + 1 + 23 j + 2 d3 j + 2
Convince yourself that that equation computes the same n as the first equation I gave.
I assume you know that xa + b = xa xb. So we can separate those 23 j + b coefficients like this:
n = ∑ j (23 j 20) d3 j + 0 + (23 j 21) d3 j + 1 + (23 j 22) d3 j + 2
Then we can factor out the 23 j term like this:
n = ∑ j 23 j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
I assume you also know that xa b = (xa)b. So we can split the 23 j term like this:
n = ∑ j (23)j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
And we can simplify 23 to 8:
n = ∑ j 8j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
Compare this to the formula for computing the number from its octal digits, which I repeat here:
n = ∑ j 8j oj
So we can conclude this:
oj = 20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2
For example, let's take j = 2:
o2 = 20 d3×2 + 0 + 21 d3×2 + 1 + 22 d3×2 + 2 = 20 d6 + 21 d7 + 22 d8
I'm working on a program that converts between number bases. For example Octal is 8, decimal is 10. Letters A to Z could be considered as base 26.
I want to convert a number like "A" into 0, Z into 25, "AA" into 27 and "BA" into 53.
Before I start coding I'm doing it on paper so I understand the process. To start out I'm trying to convert 533 to base 26.
What algorithm is best for doing this?
You need to assign a "digit" to each letter, like:
A = 0 N = 13
B = 1 O = 14
C = 2 P = 15
D = 3 Q = 16
E = 4 R = 17
F = 5 S = 18
G = 6 T = 19
H = 7 U = 20
I = 8 V = 21
J = 9 W = 22
K = 10 X = 23
L = 11 Y = 24
M = 12 Z = 25
Then, your {20,13} becomes UN.
Converting back is UN -> {20,13} -> (20 * 26 + 13) -> 52.
By way of further example, let's try the number 10163, just plucked out of the air at random.
Divide that by 26 until you get a number less than 26 (i.e., twice), and you get 15 with a fractional part of 0.03402366.
Multiply that by 26 and you get 0 with a fractional part of 0.88461516.
Multiply that by 26 and you get 23 (actually 22.99999416 on my calculator but, since the initial division was only two steps, we stop here - the very slight inaccuracy is due to the fact that the floating point numbers are being rounded).
So the "digits" are {15,0,23} which is the "number" PAX. Wow, what a coincidence?
To convert PAX back into decimal, its
P * 262 + A * 261 + X * 260
or
(15 * 676) + (0 * 26) + 23
= 10140 + 0 + 23
= 10163
Let's take a step back for a second, and look at decimal.
What does a number like "147" mean? Or rather, what do the characters '1', '4' and '7', when arranged like that, indicate?
There are ten digits in decimal, and after that, we add another digit to the left of the first, and so on as our number increases. So after "9" = 9*1, we get "10" = 1*10 + 0*1. So "147" is 1*10^2 + 4*10 + 7*1 = 147. Similarly, we can go backwards - 147/10^2 = 1, which maps to the character '1'. (147 % 10^2) / 10 = 4, which maps to the character '4'. And 147 % 10 = 7, which maps to the character '7'.
This works works for any base N - if we get the number 0, that maps to the first character in our set. The number 1 maps to the second character, and so on until the number N-1 maps to the last character in our set of digits.
You convert 20 and 13 to the symbols that represent 20 and 13 in your base 26 notation. It sounds like you are using the letters of the alphabet so, that would be UN (where A is 0 and Z is 25).
What language are you writing this in? If you're doing this in Perl you can use the CPAN module Math::Fleximal that I wrote many years ago while I was bored. If you're using a language with infinite precision integers, then life becomes much easier. All you have to do is take characters, convert them into an array of integers, then do the calculation to turn that into a number.
I am given a number N, and i must add some numbers from the array V so that they wil be equal. V is consisting of numbers that are all powers of 3:
N = 17
S = 0
V = 1 3 9 27 81 ..
I should add numbers from V to N and S in order to make them equal. The solution to the example above is :
17 + 1 + 9 = 27, 27, 1 and 9 are taken from V, a number from V can be taken only once, and when taken it's removed from V.
I tried sorting V and then adding the biggest numbers from V to S until S has reached N, but it fails on some tests when it's like:
N = 7
S = 0
V = 1 3 9 27
So the solution will be:
7 + 3 = 9 + 1
In examples like this i need to add numbers both to N and S, and also select them so they become equal.
Any idea of solving this ? Thanks.
Write N in base 3: 17 = 2*1 + 2*3 + 1*9
Find the first power of 3 with coefficient 2, in this case 1.
Add this power of 3: 17 + 1
Repeat until all coefficients are 0 or 1.
17 = 2*1 + 2*3 + 1*9
17 + 1 = 2*9
17 + 1 + 9 = 27
7 = 1*1 + 2*3
7 + 3 = 1*1 + 1*9