# Divisible functionWatch

Announcements
Thread starter 1 month ago
#1
I’ve written a function to check if a number is divisible by 3 how do I write one that checks if a number is not divisible by 3
Posted on the TSR App. Download from Apple or Google Play
0
reply
1 month ago
#2
Assuming somewhere in your function you have something like:

If (x % 3) == 0

Just change it to:

If (x % 3) != 0
0
reply
Thread starter 1 month ago
#3
(Original post by yt7777)
Assuming somewhere in your function you have something like:

If (x % 3) == 0

Just change it to:

If (x % 3) != 0
I did this and it worked however when I add
Return (n%2)!=0 or (n%3)!=0 it doesn’t work have I done something wrong?
Posted on the TSR App. Download from Apple or Google Play
0
reply
1 month ago
#4
(Original post by Lauren12311)
I did this and it worked however when I add
Return (n%2)!=0 or (n%3)!=0 it doesn’t work have I done something wrong?
What language are you using?
0
reply
Thread starter 1 month ago
#5
(Original post by yt7777)
What language are you using?
Python
Posted on the TSR App. Download from Apple or Google Play
0
reply
1 month ago
#6
(Original post by Lauren12311)
Python
def divbythree(x):
if( (x % 3) != 0):
return True
else:
return False
1
reply
Thread starter 1 month ago
#7
(Original post by yt7777)
def divbythree(x):
if( (x % 3) != 0):
return True
else:
return False

This is what I have so far it works for the single one but when I add the or it doesn’t
Posted on the TSR App. Download from Apple or Google Play
0
reply
1 month ago
#8
(Original post by Lauren12311)

This is what I have so far it works for the single one but when I add the or it doesn’t
def divbythree(x):
if( ((x % 3) != 0) or ((x % 2) != 0) ):
return True
else:
return False

So this would be not divisible by either 2 or 3
0
reply
1 month ago
#9
(Original post by Lauren12311)
I did this and it worked however when I add
Return (n%2)!=0 or (n%3)!=0 it doesn’t work have I done something wrong?
That is technically and syntactically correct, but are you sure that it's logically correct?

The logical correctness of that depends what results you're expecting -- i.e. are you sure you intended to use a logical OR rather than a logical AND? This expression will give you even-numbered multiples of 3, are you sure that's what you want?

for example, consider this table which compares 'OR' with 'AND' for the numbers 1 through to 9:
Code:
```+---+------+------+--------------+---------------+
| n |  % 2 |  % 3 |  % 2 OR  % 3 |  % 2 AND  % 3 |
+---+------+------+--------------+---------------+
| 1 | TRUE | TRUE |     TRUE     |     TRUE      |
| 2 | FALSE| TRUE |     TRUE     |     FALSE     |
| 3 | TRUE | FALSE|     TRUE     |     FALSE     |
| 4 | FALSE| TRUE |     TRUE     |     FALSE     |
| 5 | TRUE | TRUE |     TRUE     |     TRUE      |
| 6 | FALSE| FALSE|     FALSE    |     FALSE     |
| 7 | TRUE | TRUE |     TRUE     |     TRUE      |
| 8 | FALSE| TRUE |     TRUE     |     FALSE     |
| 9 | TRUE | FALSE|     TRUE     |     FALSE     |
+---+------+------+--------------+---------------+```
Notice how your current expression using the logical 'OR' operator will always evaluate to TRUE unless 'n' is evenly divisible by 2 as well as 3

So, the cases where it'll evaluate to FALSE would be even-numbers divisible by 3. i.e. 6, 12, 18, 24, etc.
Last edited by winterscoming; 1 month ago
0
reply
Thread starter 1 month ago
#10
(Original post by winterscoming)
That is technically and syntactically correct, but are you sure that it's logically correct?

The logical correctness of that depends what results you're expecting -- i.e. are you sure you intended to use a logical OR rather than a logical AND?

for example, consider this table which compares 'OR' with 'AND' for the numbers 1 through to 9:
Code:
```+---+------+------+--------------+---------------+
| n | n!=2 | n!=3 | n!=2 OR n!=3 | n!=2 AND n!=3 |
+---+------+------+--------------+---------------+
| 1 | TRUE | TRUE |     TRUE     |     TRUE      |
| 2 | FALSE| TRUE |     TRUE     |     FALSE     |
| 3 | TRUE | FALSE|     TRUE     |     FALSE     |
| 4 | FALSE| TRUE |     TRUE     |     FALSE     |
| 5 | TRUE | TRUE |     TRUE     |     TRUE      |
| 6 | FALSE| FALSE|     FALSE    |     FALSE     |
| 7 | TRUE | TRUE |     TRUE     |     TRUE      |
| 8 | FALSE| TRUE |     TRUE     |     FALSE     |
| 9 | TRUE | FALSE|     TRUE     |     FALSE     |
+---+------+------+--------------+---------------+```

This is what I want the part B but something is definitely not working 😩
Posted on the TSR App. Download from Apple or Google Play
0
reply
1 month ago
#11
(Original post by Lauren12311)

This is what I want the part B but something is definitely not working 😩
Yes, your logic is inverted -- think about it carefully

a number is "not divisible by 2, 3 or 5" if ALL of the following conditions are true:
- Not divisible by 2
- Not divisible by 3
- Not divisible by 5

in order to build an expression where ALL conditions are true, you're Essentially saying: "n is Not divisible by 2, AND n is not divisible by 3, AND n is not divisible by 5"

Remember that each individual condition is evaluated on its own for a true/false expression, so you end up with logical expression which would be something like

1 = TRUE AND TRUE AND TRUE = TRUE
2 = FALSE AND TRUE AND TRUE = FALSE
3 = TRUE AND FALSE AND TRUE = FALSE
4 = FALSE AND TRUE AND TRUE = FALSE
5 = TRUE AND TRUE AND FALSE = FALSE
6 = FALSE AND FALSE AND TRUE = FALSE
7 = TRUE AND TRUE AND TRUE = TRUE
8 = FALSE AND TRUE AND TRUE = FALSE
9 = TRUE AND FALSE AND TRUE = FALSE
etc.
Last edited by winterscoming; 1 month ago
0
reply
Thread starter 1 month ago
#12
(Original post by winterscoming)
Yes, your logic is inverted -- think about it carefully

a number is "not divisible by 2, 3 or 5" if ALL of the following conditions are true:
- Not divisible by 2
- Not divisible by 3
- Not divisible by 5

in order to build an expression where ALL conditions are true, you're Essentially saying: "n is Not divisible by 2, AND n is not divisible by 3, AND n is not divisible by 5"

Remember that each individual condition is evaluated on its own for a true/false expression, so you end up with logical expression which would be something like

1 = TRUE AND TRUE AND TRUE = TRUE
2 = FALSE AND TRUE AND TRUE = FALSE
3 = TRUE AND FALSE AND TRUE = FALSE
4 = FALSE AND TRUE AND TRUE = FALSE
5 = TRUE AND TRUE AND FALSE = FALSE
6 = FALSE AND FALSE AND TRUE = FALSE
7 = TRUE AND TRUE AND TRUE = TRUE
8 = FALSE AND TRUE AND TRUE = FALSE
9 = TRUE AND FALSE AND TRUE = FALSE
etc.
This worked perfectly thank you so much
Posted on the TSR App. Download from Apple or Google Play
0
reply
Thread starter 1 month ago
#13
(Original post by yt7777)
def divbythree(x):
if( ((x % 3) != 0) or ((x % 2) != 0) ):
return True
else:
return False

So this would be not divisible by either 2 or 3
Thank you!
Posted on the TSR App. Download from Apple or Google Play
0
reply
X

Write a reply...
Reply
new posts

Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### University open days

• University of Bath
Undergraduate Virtual Open Day Undergraduate
Sat, 23 Feb '19
• Ravensbourne University London
School of Design, School of Media Further education
Sat, 23 Feb '19
• Leeds Trinity University
PGCE Open Day Further education
Sat, 23 Feb '19

### Poll

Join the discussion

Yes (14)
5.93%
No (222)
94.07%

View All
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.