Questions tagged [modulo]
The modulo (sometimes called modulus) operation finds the remainder of division of one number by another. It's typically represented by the percent ('%') character in programming languages.
1,563
questions
0
votes
0
answers
20
views
how can i start 2 python's file at the same time?
import idiota
import schermogira
"idiota" and "schermogira" are 2 python files created by me, I want to run them at the same time but I can't, each time first executes one and ...
0
votes
1
answer
51
views
I was writing a code to enter a number and then reversing it, but the modulo isn't working it seems. What could have gone wrong here?
My concern is only with why the modulo (%) isn't working. Please don't comment on the code on it's entirety. My code is as displayed below.
import java.util.Scanner;
public class Main {
public ...
-3
votes
0
answers
23
views
fast method for determining whether one integer is a multiple of another [duplicate]
I have a very simple function whose inputs are two positive integers (m and n) and whose output is a boolean which represents whether or not the first integer is a multiple of the second. The ...
0
votes
1
answer
29
views
PHP reverting a modulo math function
I have a function that generates a number with an input in PHP:
$number = 1;
$result = $number * 683567 % 1000000;
// OUTPUT: 683567
$number = 2;
$result = $number * 683567 % 1000000;
// OUTPUT: ...
0
votes
1
answer
70
views
Generating the sequence 0, 0, 1, 1, 0, 0, 1, 1, ... in Python [duplicate]
Using % 2 gives me the alternating sequence [0, 1, 0, 1, ...]
seq = []
for i in range(10):
e = i % 2
seq.append(e)
Is there a way to generate the sequence [0, 0, 1, 1, 0, 0, 1, 1,...] by ...
1
vote
1
answer
40
views
LUA, modulo and "number has no integer representation"
I'm newbie in LUA. I want to simple display some time vaule
string_buf = string.format ( "%dh %dmin" ,Mode[2]/60,Mode[2]%60 )
But my device still inform me that at this line "#2 number ...
0
votes
3
answers
46
views
Split an array into smaller chunks of arrays with the length provided by size. Need explanation for solution with modulo
So I was solving this challenge:
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
1. (['a', 'b', 'c',...
-1
votes
1
answer
27
views
Decode function on caesar cypher is weird when using modulo
I'm currently making a caesar function and in the process of optimization, ran across a really weird problem when decrypting an encoded text. For example, encoding civilization with a shift value = 5 ...
0
votes
0
answers
57
views
power modulus operations on an byte array
How can I apply a power modulus to a byte array?
Example:
a = 0x449F
b = 0x0002
c = 0xffff
a^b % c = 0xed25
Now lets put this into an array
uint8_t a[] = { 0x44, 0x9F };
uint8_t b[] = { 0x00, 0x02 };...
0
votes
2
answers
44
views
Modulo alternative Lua
I don't have much coding experience so I don't really know of an efficient alternative to modulo, the issue I have is that I want to have the same funcionality but witouth it ever returning zero if ...
0
votes
1
answer
45
views
How to group data which have same Id number in SQL
Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
The STATION table is described as follows:
...
0
votes
1
answer
26
views
Getting 0 for Long number modulus with
getting 0 when appiling modulus for the return value
class Solution {
long ans = 0;
long m = 1000000007L;
long[][][] dp;
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {...
-1
votes
1
answer
29
views
Modulo in PHP does not give expected result
<?php
echo (9000000000 % 1000);
?>
Returns 408, while I was expecting 0
What am I doing wrong?
Kind regards
0
votes
2
answers
64
views
Populate array by recycling a smaller array starting with a particular value
I need to populate an array with a predefined number of elements ($vacancies) using a pre-existing array of values ($list_order_id).
The source array may not contain enough values to meet the required ...
1
vote
2
answers
46
views
Print triangular number patterns with loops and modulus calculation
I need to print a number pattern like this:
1
12
123
1234
2
23
234
2341
3
34
341
3412
4
41
412
4123
My code:
for($i=1; $i<=4; ++$i) {
for($j=1; $j<=$i; ++$j) {
echo $j;
}
...
0
votes
2
answers
46
views
python how to get truncated modulo vs floored modulo
I get Truncated versus floored division in Python can be used like this Truncated versus floored division in Python
we use / vs //
>>> from __future__ import division
>>> -1 / 2
-0.5
...
0
votes
1
answer
173
views
How to get the correct output in Modulo (10^9 + 7) format?
I am working on this code challenge:
Problem Description
Given 2 integers x and n, you have to calculate x
to the power of n, modulo 10^9+7 i.e. calculate (x^n) % (10^9+7).
In other words, you have ...
0
votes
3
answers
110
views
How to approach and understand a math related DSA question
I found this question online and I really have no idea what the question is even asking. I would really appreciate some help in first understanding the question, and a solution if possible. Thanks!
...
0
votes
0
answers
28
views
Problems With The Modulus Operator (%) [duplicate]
How do i get the correct value for the mod between 180 and 3.6?
180 divided by 3.6 returns an integer value of 50, which has a remainder of 0.
So why does the operation180 % 3.6
return me 3.5555556?
0
votes
1
answer
37
views
Check if Large Float is Even or Odd
I have tried using the modulo operator for small floats and it works perfectly fine.
For example: 5 % 2 returns 1.
But for other large floats that are read as exponential in python it just returns 0 ...
0
votes
0
answers
9
views
Modulo-operator in Python using "OR" condition. Project-Euler [duplicate]
all. Just practicing to code small problems at the Project Euler #1.
The point was to check number using modulo-operator that it is multiple of 3 and 5.
I've tried:
x = list(range(1000))
s = 0
for i ...
-1
votes
1
answer
55
views
Inconsistent modulus result in python [duplicate]
I've got a simple list "passage" that contains strings. Goal is to join these split words into lines of 5 words each. Using modulus 5 (against index value + 1 to simplify) as test of when ...
2
votes
1
answer
138
views
Floating point modulo *completely* "wrong"
I don't mean a tiny precision error, I mean a completely "wrong" result, for a harmless-looking calculation:
expected: 1.7306687640440686
got: 0.08453630115074517
The calculation (Try ...
-3
votes
4
answers
48
views
C# I want to find out if numbers in my List are divisable by 3 without making an array out of it [closed]
I did this already by turning the list into an arrays, but how do i do this without an array
0
votes
0
answers
150
views
The data types float and int are incompatible in the modulo operator
I can do this:
SELECT CAST(1.21 * 100 % 100 AS int)
to get the part after the decimal point.
But it won't let me do this:
DECLARE @f float;
SET @f = 1.21;
SELECT CAST(@f * 100 % 100 AS int)
I get ...
0
votes
1
answer
28
views
SQL CE give back summed values of timeranges
Given a table with following entries (simplified to get the idea)
timestamp | value 1 | value 2
60 12 0
120 13 0
180 10 12
240 8 0
....
...
0
votes
2
answers
78
views
How is this "number square" problem done with a modulus operator?
I have come across a very familiar problem doing some exercises and for the life of me cannot figure it out using the intended method -- the modulus % operator -- that accepts (int min, int max) ...
0
votes
0
answers
30
views
Does modulo (%) operation applied to part of the sum and applied to whole sum equal?
Is the LHS = RHS ?
((((i + k) % nums.length) + k) % nums.length) == ((i + k + k) % nums.length)
What is this property called? Does it hold true for multiplication/division as well?
-1
votes
1
answer
49
views
How can I calculate a really long number with modulo without using bcmod?
The bcmod function is deactivated and I won't be able to activate this because its not my own server.
For a reason I need to calculate an integer with a length of atleast 24 with modulo 97.
Integer ...
0
votes
1
answer
56
views
Python modulo returning wrong answer
I was attempting this question on leetcode using python3.
My solution for this problem is as follows:
class Solution:
def __init__(self):
self.memo = {}
def numRollsToTarget(...
-1
votes
1
answer
64
views
Wrapping around very large angles to [0,2*PI] adds too big numerical error [closed]
In below sample, I try different methods to wrap an angle between 0 and 2 x PI. While doing this to compute cosine of same angles, both methods cause a different amount of numerical error compared to ...
0
votes
1
answer
96
views
Confused by result of modulo in Solidity
Just going through some learning pains and wanted to know if anyone could help.
My current goal on the course I'm on is to return a bool value from a function depending on if the 2nd uint divides ...
0
votes
1
answer
95
views
Efficient way to get modulo for large number (e.g., find x for 100 mod x =1)
Alice has a integer x0 which she doesn't want Bob to know. Bob knows a very large integer y and also knows that y mod x0 = 1. So now Bob can solve the equation y mod x = 1 to get different x to see ...
0
votes
0
answers
18
views
Linear congruence calculation
I need to calculate x having the following formula:
222143612600812046617511287447004107467 = (124238402538911150679814605267508298634 * x + 177062111784129036182002943089208846590) % ...
0
votes
1
answer
33
views
Repeating/Circular Array Lookup (when sought index is greater than highest index)
Before I go any further, I know the title may be completely ambiguous and that this has bound to of been asked before, however I just can't articulate it better presently nor can I think of a known ...
0
votes
3
answers
33
views
Adding an extra element with modulus in a Javascript for loop
I'm new to Javascript and I'm having a problem with a closing div to a loop after every 3 divs in order to restart the column of divs.
Although it clearly adds the new div class="column", ...
1
vote
1
answer
57
views
Why the different results when using external modulus than built-in pow(x, y, mod) in Python 3.9
While watching John Hammond's YouTube video about "Modular Inverses in Python 3 (PicoCTF 2022 #03 basic-mod2)" I discovered the newer version of pow(x, y, mod) function in Python 3.9, which ...
0
votes
1
answer
30
views
modulo string additions interacting between print lines
Im printing 2 lines that both use \r and % to replace a either %d or %s in their own print lines. Problem is they seam to get crossed over with one another.
def pinging():
average_list = []
...
1
vote
2
answers
85
views
Why modulus is not working for float on ruby?
I am wondering that modulus/reminder(%) is not working properly.
As example:
a = 4.49
b = 449.0
when I do it b % a, it is not showing 0.0, it is showing 4.489999999999997
I want to know the root ...
0
votes
2
answers
46
views
How can I find all the valid divisors (AKA A%B == 0) of a number?
I am writing a method modded, which is defined as modded(X,Y,Z). X is a generated list composed of the numbers in a given list Z that Y % Z == 0.
An example would be for (X,8,[2,4,5,1]) to return X = [...
1
vote
3
answers
103
views
How do I return the amount of times number A can be divided by number B untill it includes a remainder?
for context here is my problem:
I have a table of parts. A part is produced from a set of materials, let's call the constant materials variable z. So this table is a table of parts all produced from a ...
0
votes
2
answers
40
views
How to handle operating on items in a list without perfectly even len()?
I'm trying to operate on every 5 items in a list, but can't figure out how to handle the remaining items if they don't divide evenly into 5. Right now I'm using modulo, but I can't shake the feeling ...
1
vote
0
answers
28
views
Leetcode 191: mod 10 versus mod 2
I am working through Leetcode's problem 191: Number of 1 Bits, and am confused as to a certain aspect of the solution. Here is the question:
Write a function that takes an unsigned integer and returns ...
9
votes
3
answers
237
views
polynomial (in n) time algorithm that decides whether N is a power
I am a computer science student; I am studying the Algorithms course independently.
During the course, I saw this question:
Given an n-bit integer N, find a polynomial (in n) time algorithm that ...
0
votes
1
answer
47
views
Modular arithmetic - Prove or disprove calculation
Hello everyone I am a computer science student, I am studying the Algorithms course independently.
During the course I saw this question:
I think it's a simple question, but I can not understand it
...
-2
votes
1
answer
33
views
I have some values from doing for looping and I want to change the remainder value to string [closed]
Here's my code
for num in range(1,11):
print(num)
if num % 3 == 0:
print("hello")
else:
pass
So I want to change every multiplication of 3 with "hello"...
0
votes
0
answers
40
views
how can i control animation using big-bang so it loops
does anyone know how i can write a function for big-bang which basically loops an animation.for my task i have an animation of a car which currently goes from left to right but ends when it touches ...
0
votes
1
answer
84
views
JAVA Bitwise XOR modulo
I have to perform bitwise XOR on array of integers and then Return the answer modulo 998244353.
I have solved the first part but I have got stuck in 2nd part. I searched on the internet but couldn't ...
1
vote
1
answer
55
views
Why Ruby doesn’t provide a `Complexe#modulo` method?
Ruby defines Numeric#%, aka Numeric#modulo to provide the remaining of receiver divided by an other Numeric. However, as the code bellow show, Complex is the only derived type which doesn’t provide a ...
-2
votes
2
answers
345
views
Using the modulo operator to get an infinite loop around an array
I'm able to come up with simple codes using the modulo operator and I understand it on an abstract level, but I'm having trouble seeing the concept/idea on the actual code.
Can anyone explain it to me ...