How to use Modulo?
This works
print(10%)
but I can't use %
in a method:
How come I get the following error?
5 Replies
I declared the n's type in is_prime's arguments and it worked perfectly well.
![No description](https://cdn.answeroverflow.com/1151955045202542705/image.png)
Since you didn’t give an explicit type it assumes
object
but since that entails things like String
understandably it would be worried. Declare it as a number like Int
and it works as Alvin showed.Oof, this is a tough one. What if
n
is based on a parameter, not an argument? I cannot think of a workaround. I tried using math.mod
but kind of went down a rabbit hole of type casting .If the parameter is an
object
and you want a modulo, this is a workaround:
Called with:
Important here, the object must be an alias, because dynamic types are not supported.
Mojo encourages by using SIMD types, which is preferred for parallel execution and tiling etc. These are the primitives to use efficiently. SIMD also has a __mod__ dunder for calculating the modulo.