# How to Check for Factor in Python

## The challenge

This function should test if the `factor` is a factor of `base`.

Return `true` if it is a factor or `false` if it is not.

Factors are numbers you can multiply together to get another number.

2 and 3 are factors of 6 because: `2 * 3 = 6`

• You can find a factor by dividing numbers. If the remainder is 0 then the number is a factor.
• You can use the mod operator (`%`) in most languages to check for a remainder

Examples:

2 is not a factor of 7 because: `7 % 2 = 1`

Note: `base` is a non-negative number, `factor` is a positive number.

## The solution in Python

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```def check_for_factor(base, factor):
return base/factor == int(base/factor)
```Code language: Python (python)```

Option 2:

``````def check_for_factor(base, factor):
return base % factor == 0
```Code language: Python (python)```

Option 3:

``````check_for_factor=lambda a,b:not a%b
```Code language: Python (python)```

## Test cases to validate our solution

``````import test
from solution import check_for_factor

@test.describe("Fixed Tests")
def fixed_tests():
@test.it("Should return True")
def should_return_true():
test.assert_equals(check_for_factor(10, 2), True)
test.assert_equals(check_for_factor(63, 7), True)
test.assert_equals(check_for_factor(2450, 5), True)
test.assert_equals(check_for_factor(24612, 3), True)

@test.it("Should return False")
def should_return_false():
test.assert_equals(check_for_factor(9, 2), False)
test.assert_equals(check_for_factor(653, 7), False)
test.assert_equals(check_for_factor(2453, 5), False)
test.assert_equals(check_for_factor(24617, 3), False)
```Code language: Python (python)```
Tags:
Subscribe
Notify of 