So, I need to convert any number to its reverse and put it in an array.
For example:
123456789 --> [9 8 7 6 5 4 3 2 1]
I managed to do it for small numbers like the example with this:
n=32134654654213
rev=(fliplr(num2str(n)));
mikos=length(rev);
array=[ ]
for i=1:mikos
array=[array,str2num(rev(i))]
end
But when I put a big number like 564465426464334345413435541 the array is always 1x18 double and does not show all the digits.
Any ideas?
edit As you tell me in comments, it is a limit of how many digits a double can hold. You are right, if I use a string input, it works as a charm. Still wondering hot to make it work as a function, with this form :
function digits = GetDigits(n)
As mentioned in the comments, you have a limit on the number of digits a double can store. The limit on a uint64 is bigger (19), but still not sufficient to store your value. I would make sure it does not happen.
function digits = GetDigits(n)
%check if the value behaves as a normal integer
if isnumeric(n)
if n==(n+1),error('n too big, supply as string instead');end
end
rev=(fliplr(num2str(n)));
mikos=length(rev);
array=[ ]
for i=1:mikos
array=[array,str2num(rev(i))]
end
end
Related
I am using converting a list of integers into bytes using the to_bytes function. As some numbers in my list are greater than 255 and can get pretty large; I arbitrarily decided to store them using 3 bytes.
So in my loop I do the following:-
for number in original_array:
byte_file_writer_delta.write(number.to_bytes(3, byteorder='big'))
So for example if I have a numbers like [1900, 1901] in my original array. When I convert it back using the following code, I get something like
[0, 7, 108, 0, 7, 109] in my output. I am trying to see the numbers 1900 and 1901 when I read them back from the file. The code I use to read back the numbers is:
byte_file_reader= open('byte_file_inverted_index.txt', 'rb')
byte_file_reader.seek(byte_offset)
mybytes=byte_file_reader.read(byte_size)
print(list(mybytes))
See if the following script may somehow help you... I believe the bytes_to_int function will do just fine for you!
i've used a few numbers for testing (original_array)
original_array = [1,2,3,4,5,6,7,10,1500,2999,50000,789456,9999999]
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
for number in original_array:
enc = number.to_bytes(3, byteorder='big')
print(enc)
dec = bytes_to_int(enc)
print(dec)
I need to create an array in pascal with an enormous size, is such thing possible ?
I know the size number, I mean it's not n or unknown, it's just a really really big number:
2 ^ 255
Can I do that or not ? and if not is there any other way I can make something like that happen ?
Thanks in advance
Update:
The peoblem I'm trying to solve is, by giving a number between 1 to 255 (including 1 & 255), I need to print all Gray Code with length of that number, for example:
giving n=2 the program should print:
00, 01, 11, 10
since I have the max number of size that I can enter I assumed I'd make an array with the max number, the thing is maybe I can solve this with recursion, but I;m very new to pascal so I don't know how can I do that (at least not yet)
here's what I meant:
type arr = array[1..255,1..MAXINT] of integer;
hints and tips can be very helpful <3
I guess it is an XY problem. Is seems useless to store such a big ammount of data in an array instead of fetching it on demand.
Here are the reasons:
The supposed array must be populated programmatically (a human being is unable to populate such an array manually) and this means you definitely have an algorithm for that purpose.
How will you allocate 10^64 GB (calculated by #Alex-K) of memory to store all this data?
Windows 7/8 Ultimate x64 has a maximum RAM limit of 192 GB and 512 GB respectively (for x32 - 4 GB only).
How much time you think it will take to initialize this array and assign values to its memebers?
Conclusion:
Why not to use a function Foo(const bt:byte; const it: cardinal): integer; intended to populate the array for the purpose of fetching the necessary values? You will fetch the data from something like Foo(100 ,32000) instead of MyArray[100, 32000].
If you have some exclusions (values that are different from those calculated by Foo changed at runtime by your user) you can ever store them in an array of TMyRec (or TList) where TMyRec = record Val: integer; Bt: byte; It: cardinal; end;.
I am not proficient in pascal but here is something I have written. It takes a numeric input and converts it in binary then into gray code.
This is the best I could do as again Pascal is not my main lang of work. :)
Uses Math;
var
a,b,d,e,f:integer;
c:array[1..255] of integer;
begin
write('input a decimal number! ');
readln(a);
write('grey code of decimal ',a,' is ');
if a<=1 then write(a) else
repeat
b:=b+1;
d:=a div 2;
c[b]:=a mod 2;
if d<=1 then
begin
b:=b+1;
c[b]:=d;
end;
a:=d;
until d<=1;
for e:=b downto 1 do
begin
//HERE YOU WOULD NEED TO EDIT AND FIX THIS, I THINK THIS WILL ONLY WORK FOR
//1-9
write(c[e -1] xor 1 shl (c[e]>>1));
end;
readln
end.
I have an excel spreadsheet with numbers from 000 to 999 and am trying to find repeated numbers inside a cell.
(So for example, printing 1 if the number is 022 , 555 or 115 and 0 if it isn't)
So far, I have not been able to find a solution.
Feel free to ask for more information and thanks in advance.
This will do: =IF(COUNT(SEARCH(REPT({0,1,2,3,4,5,6,7,8,9},2),A1))>0,1,0)
Note: If value in cell A1 contains 2 repeated digits it will show 1 else 0. You can customize the repetition limit by changing 2 in the part 8,9},2).
You could try this one if you wanted to find repeated digits not necessarily next to each other:-
=IF(MAX(LEN(A1)-LEN(SUBSTITUTE(A1,{0,1,2,3,4,5,6,7,8,9},"")))>1,1,0)
If the numbers are stored as 3-digit numbers and you wanted it to work for (e.g.) 001, would need:-
=IF(MAX(LEN(TEXT($A1,"000"))-LEN(SUBSTITUTE(TEXT($A1,"000"),{0,1,2,3,4,5,6,7,8,9},"")))>1,1,0)
If your data is in Range "A1:A100" and you want to locate repeated numbers in the range for instance, enter =IF(COUNTIF(A:A,A1)>1,1,0) in cell B1 and fill down. But if you want to check repetitions of specific numbers like 022, 555 or 115, enter =IF(OR(AND(A1=022,COUNTIF(A:A,A1)>1),AND(A1=555,COUNTIF(A:A,A1)>1),AND(A1=115,COUNTIF(A:A,A1)>1)),1,0) in cell B1 and fill down.
being a number, use arithmetics to break it into digits and then check if all are different.
the formula is
=INT(NOT(AND(INT(A1/100)<>INT(MOD(A1,100)/10),INT(A1/100)<>MOD(A1,10),INT(MOD(A1,100)/10)<>MOD(A1,10))))
let's analyze it step by step
first, INT(A1/100) extracts the first digit (the integer division by 100); then INT(MOD(A1,100)/10) extracts the second digit (the integer division by 10 of the modulo 100); and MOD(A1,10) extracts the last digit (the modulo 10).
next there are the three comparisons of difference <> first with second, second with third and first with third, combined with AND() and finally take the result, negate it NOT() and transforming it into an integer 0 or 1 with INT()
I have two arrays of decimals with the same number of indexes in each. How can I right-align the LSB in each column like this?:
359230595 10
1746442051 8
1170647010 8
202212421 7
800051251 7
1112147574 8
1135948848 8
3367006 5
3869426816 7
Either using printf, or even better would be getting each line into a string array so I can output it to another .txt file more easily.
I feel confident that this has been asked and answered before. To make it easy on you, however...
printf has an alignment operation that you can use. You can find it if you read the manual page. For example:
printf("%*d %*d\n", 10, x, 10, y);
Note that the * has been inserted where you would typically find a precision marker. The * is used as a placeholder for you to indicate the maximum length for that field. In this case, this will produce two right-aligned columns 10 spaces in width, separated by one space.
In this post, #boisvert mentioned that if using string as the order field's value, it is best shown for a binary string, and then gave an algorithm to calculate the average of two binary strings as follows:
Avalue = 1+0*(1/2)+1*(1/4)+1*(1/8)
Bvalue = 1+1*(1/2)+0*(1/4)+0*(1/8)
average, new value = 1+0*(1/2)+1*(1/4)+1*(1/8)+1*(1/16) new string = "10111"
content order
--------------------
A '1011'
new! '10111'
B '1100'
C '1101'
I couldn't understand these very well, what's the value of the first item putting into the DB and the items inserting before/after it? How to calculate the average between '1011' and the new value '10111', or between '111' and '1000'?
Any help is much appreciated.
The binary strings are fractions, not integers; the decimal point is always at the beginning (or after the first digit, in #boisvert's answer; it doesn't make any difference as long as the position of the decimal point is fixed. Of course, it's actually a binary point since these are binary numbers.)
To find the average:
If the strings differ in length, put enough 0s at the end of the shorter string so that it is the same length as the longer string.
Add the two strings together, using binary addition, always putting the last carry at the beginning, even if it is ยด0'. [See algorithm below].
Remove any 0s at the end.
Example 1: 1011 and 10111
Extend the first string with a 0: 10110 and 10111
Find the sum:
A: 10110
B: 10111
Carry: 101100
Sum: 101101
No trailing zeros, so the result is 101101
Example 2: 111 and 1000
1. 1110 1000
2. 10110
3. 1011
Starting off and insertion at the end:
The first item put into the database has the label 1. If at any point you need to add an item at the very beginning, use the first label with a 0 before it. Similarly, if you need to add an item at the end, use the first label with a 1 before it.
Binary addition:
Since the strings are the same length, this is easy; set Carry to 0, and scan both strings from back to front. (The output is also produced back-to-front.)
At each position:
* If the sum of Carry and the two digits is 1 or 3, output a 1, otherwise output a 0.
* If the sum of Carry and the two digits is 2 or 3, set Carry to 1, otherwise set it to 0.
When you've finished all the digits, output the value of Carry.
Practical implementation:
In practice, you wouldn't use binary strings; you'd use some fairly large base, the only requirement being that it is even. But the algorithms are the same. When constructing the representation of your numbers, you need to assign digits to characters in alphabetical order, so that the resulting strings can be sorted alphabetically without converting them to numbers; the database doesn't know how to convert to numbers, but it knows how to sort strings alphabetically.