How can I spilt long integer number into pairs?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abdulatif Alabdulatif
le 17 Avr 2014
Réponse apportée : Abdulatif Alabdulatif
le 18 Avr 2014
Hi all,
I am working to solve the problem of finding the square root by using long division operation?
I know that there is a build in square root function but in my case I have to build my own function to find a square root by using a long division!
The first step of finding the square root by long division is decompose a number in two pairs and then work from that point.
ex: 1987 --> First pair(87), second pair(19) &&&& 198 --> First pair(98), second pair(1) as we should start pairing from right to left!
Could anyone help me to solve this issue? I wish if someone already has the code for perform square root by division! This will make live easier : )
Cheers,
0 commentaires
Réponse acceptée
Niklas Nylén
le 17 Avr 2014
To split the integer x into pairs according to your description:
x = 12345
numDigitsInX = floor(log10(x))+1;
pairs = zeros(ceil(numDigitsInX/2),1);
for ii = 1:length(pairs)
pairs(ii) = mod(x,100);
x = (x-pairs(ii))/100;
end
2 commentaires
Niklas Nylén
le 17 Avr 2014
Modifié(e) : Niklas Nylén
le 17 Avr 2014
What is q? The variable 'pairs' contains the number pairs.
pairs =
45
23
1
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!