mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2010-12-05, 16:14   #2014
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
apply* only if you only go up to index b1, or b2 for each.
Yes, but you can go up to b1 rather than #b1digs.
CRGreathouse is offline   Reply With Quote
Old 2010-12-05, 16:22   #2015
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Yes, but you can go up to b1 rather than #b1digs.
I got it redefined without using b1digs or b2digs, it's still a incredibly slow script lol. Can't do convert(233,4,10) in under 4 minutes in the older version.

Last fiddled with by science_man_88 on 2010-12-05 at 16:22
science_man_88 is offline   Reply With Quote
Old 2010-12-05, 16:35   #2016
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

20C016 Posts
Default

Code:
(12:20)>convert(101,2,4)
  ***   user interrupt after 12mn, 42,922 ms.
incredibly slow indeed. don't think my CPU dropped below 50% and I think it hit past 99% once.

okay it works well for some like:
Code:
(13:06)>convert(11111,2,34)
%150 = "v[]"
(13:06)>##
  ***   last result computed in 0 ms.
because 34 is greater than 2^5-1 it is speedy but anything <= 31 for b2 may be very slow.

I figured out why lol I forgot a statement of a=a\b2 in the until loop.

now I find it doesn't work out properly.

I figured out I changed a formula and forgot to change it back lol.

Last fiddled with by science_man_88 on 2010-12-05 at 17:24
science_man_88 is offline   Reply With Quote
Old 2010-12-05, 17:26   #2017
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Well I now know why because a\b2 ==0 is wrong this is always fun. It should of been a/b2 not a\b2 I got it working now if only i can get rid of the [] in the answers. I did that stuff in my original code I do that and I have a converter that should work not only to binary but between any matchups from base 2 to 36. though I should add a check of that and if they are equal spit back the string at them lol.

Last fiddled with by science_man_88 on 2010-12-05 at 17:37
science_man_88 is offline   Reply With Quote
Old 2010-12-05, 21:06   #2018
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Code:
convert(numb1,b1,b2)={
  my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],a=0,c="");
  numb1=Vec(Str(numb1));
  forstep(y=#numb1,1,-1,
    for(x=0,b1,
      if(numb1[y]==B[x],
        a=a+(x-1)*b1^(#numb1-y)
      )
    )
  );
  until(a/b2==0,
    c=concat(B[a%b2+1],c);
    a=a\b2
  );
  c
};
when is it time for a new thread if it's needed lol.

this doesn't do better than binary for to base 2 conversion at least from base 36 lol.

Last fiddled with by science_man_88 on 2010-12-05 at 21:07
science_man_88 is offline   Reply With Quote
Old 2010-12-05, 22:44   #2019
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
convert(numb1,b1,b2)={
  my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],a=0,c="");
  numb1=Vec(Str(numb1));
  forstep(y=#numb1,1,-1,
    for(x=1,b1,
      if(numb1[y]==B[x],
        a=a+(x-1)*b1^(#numb1-y)
      )
    )
  );
  until(a/b2==0,
    c=concat(B[a%b2+1],c);
    a=a\b2
  );
  c
};
when is it time for a new thread if it's needed lol.

this doesn't do better than binary for to base 2 conversion at least from base 36 lol.
another change lol
science_man_88 is offline   Reply With Quote
Old 2010-12-06, 01:52   #2020
kar_bon
 
kar_bon's Avatar
 
Mar 2006
Germany

B5C16 Posts
Default

Another solution for this task:

Code:
\\ Convert number given in base1 into base2
convert(number,base1,base2)={
  my(B=Vecsmall("0123456789abcdefghijklmnopqrstuvwxyz"),a=0,c="");
  number=Vecsmall(number);
  forstep(y=1,#number,1,
    a*=base1;
    a+=number[y]-48-(number[y]>=97)*39;
  );
  until(a/base2==0,
    c=concat(Strchr(B[a%base2+1]),c);
    a=a\base2;
  );
c
};

Last fiddled with by kar_bon on 2010-12-06 at 01:53
kar_bon is offline   Reply With Quote
Old 2010-12-06, 01:55   #2021
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

20C016 Posts
Default

Quote:
Originally Posted by kar_bon View Post
Another solution for this task:

Code:
\\ Convert number given in base1 into base2
convert(number,base1,base2)={
  my(B=Vecsmall("0123456789abcdefghijklmnopqrstuvwxyz"),a=0,c="");
  number=Vecsmall(number);
  forstep(y=1,#number,1,
    a*=base1;
    a+=number[y]-48-(number[y]>=97)*39;
  );
  until(a/base2==0,
    c=concat(Strchr(B[a%base2+1]),c);
    a=a\base2;
  );
c
};
I just tested this and i get:

Code:
(21:54)>convert1(1111111,2,10)
%15 = "1111024"
I've highlighted in red what i think is throwing it off compared to the previous.

Last fiddled with by science_man_88 on 2010-12-06 at 02:07
science_man_88 is offline   Reply With Quote
Old 2010-12-06, 02:25   #2022
kar_bon
 
kar_bon's Avatar
 
Mar 2006
Germany

22·727 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I just tested this and i get:

Code:
(21:54)>convert1(1111111,2,10)
%15 = "1111024"
I've highlighted in red what i think is throwing it off compared to the previous.
Sorry, forgot to mention:

<number> has to be given as a string!

so convert1("1111111",2,10) will do the trick!

Advantage:

You can insert any 'number' in any base from 2 to 35 and convert in any of these bases.

For example:
convert1("1j3",20,10) gives "783"
convert1("783",10,16) gives "30f"

or in one step
convert1("1j3",20,16) gives "30f"
kar_bon is offline   Reply With Quote
Old 2010-12-06, 02:35   #2023
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by kar_bon View Post
Sorry, forgot to mention:

<number> has to be given as a string!

so convert1("1111111",2,10) will do the trick!

Advantage:

You can insert any 'number' in any base from 2 to 35 and convert in any of these bases.

For example:
convert1("1j3",20,10) gives "783"
convert1("783",10,16) gives "30f"

or in one step
convert1("1j3",20,16) gives "30f"
it can't do base 36 ? mine did.

wonder how fast each is.
tested both with the equivalent of 36^577-1 and they came up equal.

Last fiddled with by science_man_88 on 2010-12-06 at 02:41
science_man_88 is offline   Reply With Quote
Old 2010-12-06, 04:54   #2024
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

597910 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
it can't do base 36 ? mine did.
Sure it can, try
Code:
convert("3731151013004108312", 10, 36)
CRGreathouse is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do I sometimes see all the <> formatting commands when I quote or edit? cheesehead Forum Feedback 3 2013-05-25 12:56
Passing commands to PARI on Windows James Heinrich Software 2 2012-05-13 19:19
Ubiquity commands Mini-Geek Aliquot Sequences 1 2009-09-22 19:33
64-bit Pari? CRGreathouse Software 2 2009-03-13 04:22
Are these commands correct? jasong Linux 2 2007-10-18 23:40

All times are UTC. The time now is 22:43.


Fri Aug 6 22:43:30 UTC 2021 up 14 days, 17:12, 1 user, load averages: 4.78, 4.24, 3.78

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.