Solve MATLAB Exercise: Number of Positive Integers < n Divisible by a or b

In summary, the task is to write a MATLAB function that takes in three integers, n, a, and b, and outputs the number of positive integers less than n that are divisible by either a or b. The function should use the floor() and mod() functions to determine if an integer is divisible by a or b. Alternatively, a brute force approach can also be used.
  • #1
M1ZeN
17
0

Homework Statement


Write a Matlab function that takes as input three integers n, a and b, and outputs the number
of positive integers less than n that are divisible by a or b. For example, with n = 1000, a = 3
and b = 5, the output should be 466.


Homework Equations


MATLAB's use of entering in the program into editor. I do not know of any functions in which to use.

The Attempt at a Solution


I have read over my classes notes over and over again (popping adderalls). I have spent a total of three full days non stop reading and trying to understand (and yes I have had sleep). I have gone through many online tutorials for MATLAB and understanding it's features and commands. However this particular homework exercise has left me completely lost.

First. I'm writing the function into the MATLAB editor. The only work I have done so far is my attempt in defining the variables (n, a & b) for the integers and my attempt at entering in the user input command where you would put in values from the Command Window.

I do know somewhere in the program I'm going to have use the operator of less than. I'm confused on the divisible part and also how to define a command to display the output of the integer that is less than n that are divisible by a or b.
 
Physics news on Phys.org
  • #2


There are two ways to solve this, a clever way and a "brute force" way.

Some useful functions:

floor(A) : rounds down to the nearest integer.

mod(X, Y) : modulus after division - this is the same as the remainder when both X and Y are positive
If X is evenly divisible by Y, then mod(X, Y) will return 0.
 

FAQ: Solve MATLAB Exercise: Number of Positive Integers < n Divisible by a or b

How do I solve the MATLAB exercise "Number of Positive Integers < n Divisible by a or b"?

To solve this exercise, you will need to use the built-in MATLAB function "mod" which calculates the remainder after division. You will also need to use a loop to iterate through all the positive integers less than n, and check if each integer is divisible by a or b using the mod function. Finally, you can use a counter to keep track of the number of integers that meet the criteria.

Can I use any values for n, a, and b in this exercise?

Yes, you can use any positive integer values for n, a, and b. However, keep in mind that the larger the values are, the longer it may take for the program to run.

How can I make the program more efficient?

One way to make the program more efficient is to use the fact that the number of integers divisible by a or b within a certain range can be calculated using the inclusion-exclusion principle. This means that you can subtract the number of integers divisible by both a and b from the total number of integers divisible by a or b. This method will reduce the number of iterations needed in the loop.

Can I use this exercise to find the number of integers divisible by more than two numbers?

Yes, you can modify the program to find the number of integers divisible by more than two numbers. Instead of using the mod function to check for divisibility by only two numbers, you can use multiple mod functions to check for divisibility by as many numbers as you need.

Can I use this exercise to find the sum of all integers divisible by a or b?

No, this exercise only counts the number of integers divisible by a or b. To find the sum of all integers divisible by a or b, you will need to modify the program to add each divisible integer to a running total instead of using a counter.

Similar threads

Replies
1
Views
1K
Replies
10
Views
2K
Replies
13
Views
2K
Replies
1
Views
2K
Replies
4
Views
4K
Replies
10
Views
2K
Replies
1
Views
786
Replies
5
Views
3K
Back
Top