写一个C函数来确定一个数字是否具有给定的素数因子? [英] Write a C function that determines whether a number have a given prime factor?

查看:79
本文介绍了写一个C函数来确定一个数字是否具有给定的素数因子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果使用值10作为numberand 5调用函数作为primeFactor,则函数应返回1,表示5是10的素数因子。同样,如果示例中的数字为9,则函数应返回0而且我也被建议调查模数的使用..?



我尝试了什么:



我试图创建一个分解数字的函数,这样我就可以检查素数因子并确定第二个数字是否是其中之一,但我对编程是全新的,所以任何提示会很棒!

解决方案

这是一个很好的功课,所以我只给你一些提示。



a)你必须只检查直到输入的半值(表示x / 2 * 2)

b)写一些函数并使用可理解的名字和一些输出

c)使用一些测试用例



有关更详细的方法,您可以搜索互联网。

Quote:

编写一个C函数来确定一个数字是否具有给定的素因子?



如果第二个数字不是素数,结果是什么?

如果结果相同,你只需要找到第二个数字是第一个数字的因子。第二个数字是素数并不重要。



引用:

而且我也是建议调查使用模数..?



总是阅读文档。



引用:

但我完全不熟悉编程所以任何提示都会很棒!



显示你的代码以获得帮助。


For example, if the function is called with the value 10 as numberand 5 as primeFactor, the function should return 1 indicating that 5 is a prime factor of 10. Similarly, the function should return 0 if the numberin the example was 9. And also I was recommended to look into the use of modulus..?

What I have tried:

I tried to make a function that factorises a number, so that I then could check the prime factors and determine if the second number was one of those, but I am completely new to programming so any tips would be great!

解决方案

That is a nice homework for you, so I will give you only some tips.

a) you must only check til the half value of input (means x/2 * 2)
b) write some function and use understandable namens with some output
c) use some test cases

For more detailed approaches you may search the internet.


Quote:

Write a C function that determines whether a number have a given prime factor?


What is the result if second number is not a prime?
If result is same, you just have to find if second number is a factor of first one. Second number being a prime does not matter.

Quote:

And also I was recommended to look into the use of modulus..?


Always read documentation.

Quote:

but I am completely new to programming so any tips would be great!


Show your code to get help.


这篇关于写一个C函数来确定一个数字是否具有给定的素数因子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆