David Sycamore

95 Reputation

4 Badges

6 years, 12 days

MaplePrimes Activity

These are replies submitted by David Sycamore

@Carl Love Thanks, I copy pasted to new worksheet, and hit !!!  Got : "Error, (in MySeq) Vector index out of range". Any suggestion? Can you resend as Download? 

@Carl Love I added it inside the plot command, separated by a comma from the original content. Worked a charm. Also used it on the last code you did for me (primes and digit sums); worked well. Thank you very much Carl, you are a great help 


@Carl Love Thanks, worked a treat and I have tried same trick on last code you helped me with. Much appreciated. Can you suggest how to do log plot?

Thanks again,


@vv Thanks but I cant get it to run (first code ran fine). Maybe I have to add something after the end of the proc, so it executes the sequence? 


@Carl Love Thank you very much. There were some unexpected results: According to my calculation the next term after 149 (whose digisum is 14) should be 1409, (the smallest prime whose decimal representation begins with 14), however the data says 1423. Likewise the term following 41 (digisum 5) should be 503 but data says 521. Same applies to 809/811 (following 17). In all these cases the selected prime seems not the smallest, but the next one after that. Any comment?


@Carl Love Much obliged, thank you.


@Carl Love Thanks for replies and sorry my terminology appeared to cause some confusion (It just means always choosing the next term in the sequence so that it is the least of all possible choices). Both codes run no problem.

Please could you say how to adapt the first code (S1), with same sequence definition, but using sum of first n terms rather than sum of their digits? Namely: Lexico earliest sequence of distinct positive integers (first term= 1),  such that the sum of the first n terms is coprime to their concatenation. It starts as the original did, but diverges once past single digit terms:  (1,3,7,2,4,5,9,6,12...).
Thanks in advance,

@Carl Love Thank you. The Programming guide is of ongoing interest. I have not managed to find my way all around it yet but will keep at it. I did try to find answers to some of the questions I asked you, but did not succeed yet.

I did not appreciate the importance of the space after the comma, will watch out for that.

My code for k1, k2 (up to ordinal of a prime), adapted from Kitonum:

P := proc (k)

local n, k1, k2;

n := 1; k1 := 0; k2 := 0;

for n from 1 to 10^k do

if isprime(10*ithprime(n)+1) then k2 := k2+1 else k1 := k1+1 fi 

end do;

[k1, k2];

end proc;

seq(P(k), k = 1 .. 5);
   [6, 4], [76, 24], [822, 178], [8602, 1398], [88409, 11591]

It might not be all my own work, but i do understand it. (I like the output in this form, very clear to read). Can your code putput in this way ?

The k1 only case was also up to ordinal of a prime, which means the k2 is then obvious by difference. Though not of interest to you, it caused me more problems than the above, but I managed to find a solution. 

I looked up ?table, thanks. I ran your code and got the output, thanks. 

I did not mean to "quote" your code at the end of my last message. I imported it to my screen just for reference when writing to you, and did not include all of the last line. However I forgot to erase it at the end; thats all. I do understand  that the procedure ends with end proc, then comes the output. 

My dificulty understanding is not especially to do with spaces and commas, they are just symptoms. The reason is because computational science is new and very alien to me. I have had zero education in this so am a lone learner in an environment where every tiny mark on the screen is loaded with meaning, and the scope for error is vast (imagine you had to learn a foreign language: Mandarin, Russian... thats how this is for me). I do greatly appreciate your help; thank you. What I need is lots of practice and good exercises to do.


ps: When i copied your code onto my reply last time, it came in a "box" with slightly different background. That did not happen when I copied my code here today, it arrived pretty much all in one line and with some punctuation missing, which I had to tidy it up. 



@Carl Love Thank you. I have looked at the Programming guide and found it helpful. I wrote a code to find just k1 (rather than k1 and k2) using ordinal of primes, and another to find k1, k2 (ie 1 or 2) for each prime up to a limit. Both used procs and both worked. I have also looked again at your code and seen that k1,k2 are not the same as K1 and K2. Whereas all are declared local within the proc, only k1,k2 are given initial values. I note the the 'name' of the proc is k1k2 and that this is never used (called) again. I dont know what K2[p] and K1[p] mean but they seem to be assigned (using :=) to the current values of k2 and k1 respectively after the execution of each iteration of "if" within the do loop. Between od and end proc the final values of k1,K2 are delivered but now there are no square brkts [ ] following them... I tried to adapt your code to tell me the values k1,k2 in sequence format (as per Kitonum) but did not succeed yet but will keep trying. I also wrote another simple code, using a proc, not related to this task, and it also worked. So some modest progress, but still some open questions.  


k1k2:= proc(N::posint, a::posint:= 10, b::posint:= 1)
local K1, K2, p:= 1, i, k1:= 0, k2:= 0;
    for i to N do
        p:= nextprime(p);
        if isprime(a*p+b) then 
            k2:= k2+1; K2[p]:= k2
            k1:= k1+1; K1[p]:= k1
    K1, K2
end proc

@Carl Love Thank you for all 3 answers: 

1. I ran the code you just sent (up to the end of the (k1k2(10^6)): line, so not including the graph bit).  It appeared to work but the only data I got was related to usage. How to get the k1,k2 data? When I ran the first version you sent I got the full graphics but not the numerical data as per Kitonum.

2. So both K1 and K2 have linear behaviour? I was expecting something logarithmic because this is about primes and pi(n) ~ n/log(n) and k1(n)+k2(n)=pi(n). I must have missed something...

3.I liked this answer; fair enough dont teach me bad stuff thats very respectable. I try to avoid procs because I cannot figure out how they work and when to use them.  If I could only get past that key hurdle you might never hear from me again (believe me I do try hard before asking here; every time). The answer always includes a proc, always works  but I rarely get to understand why, and dont like to ask again.  I do see the general structure (proc, do, if, fi,od,end proc ) but get thrown by the details. Examples: When you say "N::posint" does this tell the proc that you intend to use a number called N which is declared as a positive integer and that its value will be given later? What is the meaning of  "k2[p]:=k2", etc? After od you write k1,k2 and I dont see what is this command is doing. Then, after the end of the proc you write (K1,K2):= this looks to me that you are assigning values here, rather than calling for the latest versions of them. Kitonum used a seq command to call up the output, so I could see for up to 10^k, the data [k1,k2], and he used that square brkt format after the end of his proc, whereas you use round brkts. These differences are quite a challenge or a learner. Your  way is clearly different from his, perhaps because the aim was more to print out the graph rather than the numbers behind them?. 
I'm really sorry about all these extra questions.

Is there a decent book on Maple coding?

@Carl Love Thanks for your solution, I sent some questions to Kitonum, please comment also if you wish. 


@Kitonum Thanks for your solution, I hope you wont mind some questions:

1. Instead of counting the number of "1" and "2" primes up to 10^k for some k= 1.2.... can your code be adapted to do the same count up to the 10^k-th prime ( or more generally up to ithprime(N))? Done this way the sum of a,b in [a,b] should be 10^k (or N).

2. I noted you got ~ straight lines with log scale on the vertical axis. However, unless I am mistaken, the graph produced by Carl  did not do this, and yet also got straight lines. What have I missed?

3. Is there any way to do a "counting up" code like this one, but without using a proc? (my attempt was an if statement within a do loop, no proc); it didnt work.

@Carl Love 

No calculation error, transcription mistake.  Thanks to everyone who responded; great response and so quick.

@vv Sorry I dont understand your reply. What must I do to get the same results for P(n)  as you did, using your latest code ?


@vv Thanks very much for that. 
I copy pasted it, put P(50) and hit return.The output I got  was every prime <=50 (ie 2,3,...,47), whereas you  got the correct answer (2,3,5 7,11,13,19). Could this be because my version is 2017? Please advise any changes to make it work.






1 2 3 4 5 6 Page 2 of 6