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

Click For Summary
SUMMARY

The discussion focuses on creating a MATLAB function that counts the number of positive integers less than a given integer n that are divisible by two other integers a and b. The example provided illustrates that for n = 1000, a = 3, and b = 5, the expected output is 466. Key MATLAB functions mentioned include floor(A) for rounding down and mod(X, Y) for determining divisibility. The user expresses confusion regarding the implementation of the function, particularly in handling user input and displaying the output.

PREREQUISITES
  • Basic understanding of MATLAB programming and syntax.
  • Familiarity with functions and variable definitions in MATLAB.
  • Knowledge of conditional operators, specifically the less than operator.
  • Understanding of modulus operations for checking divisibility.
NEXT STEPS
  • Learn how to define and use functions in MATLAB.
  • Research the use of loops and conditional statements in MATLAB.
  • Explore MATLAB's input and output functions for user interaction.
  • Study examples of similar problems involving counting and divisibility in MATLAB.
USEFUL FOR

Students learning MATLAB, particularly those tackling programming assignments involving mathematical computations and logic. This discussion is also beneficial for educators seeking to assist students with similar homework challenges.

M1ZeN
Messages
17
Reaction score
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


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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K