![]() |
[QUOTE=science_man_88;240134]apply* only if you only go up to index b1, or b2 for each.[/QUOTE]
Yes, but you can go up to b1 rather than #b1digs. |
[QUOTE=CRGreathouse;240135]Yes, but you can go up to b1 rather than #b1digs.[/QUOTE]
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. |
[CODE](12:20)>convert(101,2,4)
*** user interrupt after 12mn, 42,922 ms.[/CODE] 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.[/CODE] 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. |
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.
|
[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 };[/CODE] 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. |
[QUOTE=science_man_88;240178][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=[COLOR="Red"]1[/COLOR],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 };[/CODE] 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.[/QUOTE] another change lol |
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 }; [/code] |
[QUOTE=kar_bon;240211]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, [COLOR="Red"] a*=base1; a+=number[y]-48-(number[y]>=97)*39; [/COLOR] ); until(a/base2==0, c=concat(Strchr(B[a%base2+1]),c); a=a\base2; ); c }; [/code][/QUOTE] I just tested this and i get: [code](21:54)>convert1(1111111,2,10) %15 = "1111024"[/code] I've highlighted in red what i think is throwing it off compared to the previous. |
[QUOTE=science_man_88;240212]I just tested this and i get:
[code](21:54)>convert1(1111111,2,10) %15 = "1111024"[/code] I've highlighted in red what i think is throwing it off compared to the previous.[/QUOTE] 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" |
[QUOTE=kar_bon;240213]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"[/QUOTE] 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. |
[QUOTE=science_man_88;240214]it can't do base 36 ? mine did.[/QUOTE]
Sure it can, try [code]convert("3731151013004108312", 10, 36)[/code] |
| All times are UTC. The time now is 23:06. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.