Carl Love

Carl Love

28100 Reputation

25 Badges

13 years, 104 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@Rouben Rostamian  

If you're interested, here's some shortenings of your code, each procedure becoming a one-liner.

 

Happy numbers

See http://www.mapleprimes.com/questions/208360-The-Happy-Ages-Problem

 

Rouben Rostamian, 2016-01-12

restart:

Returns the sum of squares of the decimal digits of an integer:

f:= (n::posint)-> `+`((convert(n, base, 10)^~2)[]):

Example:

f(123);

14

It can be shown that starting with any positive integer n, the sequence of iterates of f, that is , n, f(n), f(f(n)), ..., will ultimately hit either 1 or 4.  (See https://en.wikipedia.org/wiki/Happy_number for proof.)

 

The following function returns the list [n, f(n), f(f(n)), ... ], where the final term is either 1 or 4.

(h,h(1),h(4)):= (((n::posint)-> [n, h(f(n))[]]), [1], [4]):

Examples

h(7);

[7, 49, 97, 130, 10, 1]

h(88);

[88, 128, 69, 117, 51, 26, 40, 16, 37, 58, 89, 145, 42, 20, 4]

If the list returned by h(n) ends in 1, then n is called a happy number.  This function lists all happy numbers from 1 to n:

happy_numbers:= (n::posint)-> select(n-> h(n)[-1]=1, [$1..n]):

Example:

happy_numbers(100);

[1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100]

 

 

Download happy-numbers_mod.mw

@farahnaz 

My comments weren't intended to be a fix for your problem. They were intended to make tracking down the problem easier.

Why do you use evalf(ApproximateInt(...)) rather than the more normal evalf(Int(...))? The latter will return a much more accurate result. The former doesn't respect the accuracy specified by Digits.

You mentioned dsolve, but I can't find any dsolve in your code. If it's there, please highlight it with a comment.

@Kitonum RREF stands for reduced row-echelon form.

It'd be nice if you'd use comments to indicate in your lengthy worksheet where the reader can find f or D5. Also see my Reply in this other recent thread (indeed, it looks like you may be working on the same problem as that poster): "error in calculate Determinant of matrix". My point 4 in that Reply doesn't apply to you, but the others do.

Your worksheet is difficult to read and to debug for several reasons:

  1. First and foremost, separate the commands into separate execution groups.
  2. Use a lower value of Digits until the bugs are worked out.
  3. End commands with semicolons rather than colons so that you can see their output. The infinity must occur before you take the determinant. If you can see the output of the commands, then you'll see where it occurs.
  4. Do not make assignment statements of the form f(x):= y in 2D input. If you're defining a function, use f:= x-> y. On the off chance that you're making a remember table assignment, I advise switching to 1D input. It's not fair to ask the readers of your worksheet to decide between the two, and there's no way to set a default answer to the question.

If you make these changes, I'll be happy to take another look at your worksheet.

@sideshow Your attempt to use or is nonsense to Maple, although technically it doesn't violate any syntax rule, so there's no error message (until you try to use the function!). The prefix form `if` is an operator with exactly three operands. To use it for an elif situation, you need to nest them:

g:= (x,y)-> `if`(x=0.5, 200*y, `if`(y=0.5, 200*x, 'procname'(args))):

For a short chain of conditions, nested `if`s are shorter to type. But for a long chain of elifs, the parentheses required make that unbearable and you're better off using the long form if-then-elif-else-end if as in the Reply above.

But by applying logic, you can often combine conditions. For example, the four conditions from the previous Reply can be combined into a single condition like this:

g:= (x,y)-> `if`(x*y=0 or 0.5 in {x,y}, 400*x*y, 'procname'(args))):

Many complicated compound conditional statements can be made more efficient (and often less readable) by using some creative logic like this.

@sideshow 

In that case, it's better to use if-then-elif-then-else-end if:

g:= (x,y)->
     if x=0 or y=0 then 0
     elif x=0.5 then 200*y
     elif y=0.5 then 200*x
     else 'procname'(args)
     end if
:

@Rouben Rostamian  

Assuming that the OP has faithfully encoded the algorithm (which I haven't checked mostly because of annoyance with 2D-input issues and also because I don't have the book), the significant difference is that the OP uses Maple's default 10-digit software-float arithmetic and Rouben uses hardware-float arithmetic (which carries about 18 digits) for the Matrix operations. It's very likely that the book's example was executed with this same IEEE 754 arithmetic, which is called "double" or "double precision" in many languages.

@acer Yes, Acer, thanks. I missed that EQ was only the loop index.

Yes, you want (1)=~ 0, where (1) is the equation label reference.

@mskalsi 

You only need the command that I gave. You don't need a for loop. Just six characters:

EQ=~0;

@hind Like I said, the error is at s[2]. That is a "correct indexed subscripted name" in your program!

Please don't include multi-line plaintext Maple output in your posts unless you know how to format it correctly. It's totally unreadable. For the vast majority of questions, we only need to see the input in order to answer them. If we need to see some output, we'll ask for it, in which case an attached worksheet in probably the best option.

I'm about to edit the unreadable output from your post.

@Preben Alsholm 

There's a good chance that those extra multiplication signs came from putting an extra space after each function's name in 2D input. So make sure that you enter, for example, sech(-mu) rather then sech (-mu).

@vv 

Keller box is, I belive, a numerical PDE method, not an ODE method. Since Maple's built-in numerical PDE solver is quite weak, it'd be no surprise at all if the OP has one that it can't solve.

Do you want to see a step-by-step solution proving that the series converges (standard calculus II material)? Or do you want a step-by-step solution showing how to approximate the sum of the series to any given guaranteed accuracy? Either is easy for this particular series.

First 441 442 443 444 445 446 447 Last Page 443 of 709