Jump to content

I need some help with MATLAB..

- - - - -

  • Please log in to reply
12 replies to this topic

#1
Ravi Mudhoo

Ravi Mudhoo

    Newbie

  • Members
  • Pip
  • 7 posts
Can someone please help me.
i need to write codes in MATLAB.
and i dont know how to proceed.
" Using the multiplicative inverse of 10 mod 23, write code to check whether a number is divisible by 23"
i have an example but it checks if number is divisible by 7.
Please help me.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Standard response: what do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Ravi Mudhoo

Ravi Mudhoo

    Newbie

  • Members
  • Pip
  • 7 posts
clear;

num=input('num');
k=0;
% divisibility rule by 7
% remaining digits- 2(last digit)
while num>0
%for k=1:2,
    % express num1 into linear combination of powers of 10
    num1=num;
    i=0;
    rk=[];
 num1array=[];
 num1arraynew=[];
while num1>0
r=rem(num1,10);
q=floor(num1/10);
i=i+1;
rk(i)=r;
num1=q;
end
ilast=i;
for i=1:ilast,
    num1array(ilast-i+1)=rk(i);
end
% array of coefficient of powers of 10
%
num1last=num1array(ilast);
for i=1:ilast-1,
num1arraynew(i)=num1array(i).*10^(ilast-i-1);
end
num1new=sum(num1arraynew)-2*num1last;
k=k+1;
%fprintf('\n k num1array numlast num1new\n');
%fprintf('%d %d %d %d \n',k,num1array,num1last,num1new);
k, num1array, num1last, num1new
num=num1new;
end
k
 r3=rem(num1new,7);
if (r3==0)
    fprintf(' number is divisible by 7 ');
else
    fprintf('\n number is not divisible by 7 \n');
end


I ve this code for divisibilty by 7.
i know i should start by having the multiplicative inverse of 10mod23 and the answer is 7.
now how to modify the code given to make it check for numder divisible by 23, i'm stuck :S

Edited by WingedPanther, 18 October 2011 - 06:41 AM.
add code tags (the # button)


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
In the last few lines, have you tried changing 7 to 23?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Ravi Mudhoo

Ravi Mudhoo

    Newbie

  • Members
  • Pip
  • 7 posts
Dude truely..are you talking about the fprintf lines!!!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
r3=rem(num1new,7);

Realize, I've NEVER used MATLAB, and the assignment directions look strange, at best. Normally, when I want to check divisibility I just use the modulus operator of whatever language I'm using.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Ravi Mudhoo

Ravi Mudhoo

    Newbie

  • Members
  • Pip
  • 7 posts
okay.
yeah i tried but it didnt work.
where can i get help for this man??

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Well, I would start by clearly stating what SHOULD be done. I'd have to do some research to even know what your directions are asking you to do.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Ravi Mudhoo

Ravi Mudhoo

    Newbie

  • Members
  • Pip
  • 7 posts
let me give you the whole thing. and how it is set.

1. Find the multiplicative inverse of 10 mod 23:

2. Hence, write a well-documented Matlabr code to check whether
a number is divisible by 23.

Your code must input
- the number,


and give the outputs:
- whether the number is divisible by 23 or not.


the multiplicative inverse of 10 mod 23 is 7.
i think the code should be edited somewhere.and the answer will be found.
i'm searching also how to solve this problem.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
My normal understanding, assuming the real number system, is that 10 mod 23 is 10. It's multiplicative inverse is 1/10. How does that help me check whether a number is divisible by 23?

If you're doing Number Theory, then 7 would be the inverse of 10 in Z/Z23.

Stating what SHOULD be done would normally be the algorithm, in pseudocode. Forget MATLAB for a moment. How would you check it by hand?
Looking here: Divisibility rule - Wikipedia, the free encyclopedia it appears that n is divisible by 23 iff n mod 10 + n div 10 * 7 is divisible by 23. If you repeat the process a few times, as needed, it should get you down to one of 23, 46, or 69.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Ravi Mudhoo

Ravi Mudhoo

    Newbie

  • Members
  • Pip
  • 7 posts
Yes i'm doing Number Theory.

By hand!!!
The normal procedure would be adding 7 times the last digit to the rest as stated by Wikipedia.

Here’s one example, to test divisibility by 23. Since 23 x 3 = ( 10 x 7 ) - 1 we know that 10a + b will be divisible by 23 if and only if a + 7b is divisible by 23. Here’s the test with n = 146096 :

14609 + ( 7 x 6 ) = 146351
14635 + ( 7 x 1 ) = 1472
147 + ( 7 x 2 ) = 161
16 + ( 7 x 1 ) = 23


and we have shown divisibility by 23, without any tiresome division.

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
So, with that in mind, you need a loop that terminates when your number is less than 70. Then just look to see if your result is one of 23, 46, or 69.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users