factors issue

Discuss your code, get questions answered

factors issue

Postby d_motto » Wed Apr 18, 2012 11:08 am

I am attempting to convert the factors from a list of factor - power pairs to a list of factors (example: 12345 factors should give [823 1 5 1 3 1] and I want 823 5 3; 256 factors should give [2 8] and I want 2 2 2 2 2 2 2 2. Sometimes, factors is returning the entries in the stack instead of a vector, so this program does not work:

≪ @dup factors toElements 2. / { } 1. ROT START DUP 1. 4. ROLL START PICK3 + NEXT NIP NIP NEXT≫

Instead of returning the answer, it is returning a vector of two or so entries, the second of which starts with the angle character.

Is there something I am doing wrong?
David Motto
d_motto
 
Posts: 35
Joined: Tue Apr 17, 2012 9:19 am

Re: factors issue

Postby oliver » Thu Apr 19, 2012 11:26 am

Hi David,

Sometimes, factors is returning the entries in the stack instead of a vector...


Are you sure this happens with factors? Can you give me an example number (or do you think it's intermittent)?
The command factor sometimes returns an expression, and sometimes a number (when the input is prime). But factors should always return an array. Note, though, the array will only have two entries for primes. Make sure your code handles that case.

Instead of returning the answer, it is returning a vector of two or so entries, the second of which starts with the angle character.


I see. That indicates your coordinate system is set to polar/cylindrical or spherical. Go into the Mode menu and tap rect (or issue the command RECT) to switch back to the default of the "Rectangle" (Euclidean) coordinate system. Only 2- and 3-element vectors are affected by this setting. I agree, it seems a bit weird, but it's very handy when you use a non-rect coordinate system, as it permits you to see results without having to convert them manually (and it's backwards compatible to HPs).

Hope this solves your issue with the function.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: factors issue

Postby d_motto » Fri Apr 20, 2012 7:44 pm

Well, I will see if this continues to happen, now that I have fixed the other program according to your directions.

It was very strange: when the agemeJS program worked, the program I wrote to manipulate the "factors" results would work; when agemeJS was put on line 1 instead of executing, "factors" would return the results in the stack as if toElements or fromVec had been run on it (I cannot recall if the number of factor-power pairs was in the stack as well).

I could not give you an example of when this would happen (it did not matter what number I tried; generally 555 would be the one I would test it with), but it was definitely the "factors" function.

Thank you again for looking at my code. If I ever find any REAL bugs, I will let you know.

Also, thanks for fixing the Examples shared folder.

David Motto
d_motto
 
Posts: 35
Joined: Tue Apr 17, 2012 9:19 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron