admin管理员组文章数量:1289384
I'm stuck with the JavaScript code I am using for solving a problem which states:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
(this is not homework, is an online coding/mathematical challenge)
So I came up with this solution:
<html>
<head>
<script type="text/javascript">
// This function checks whether it's possible to divide the prime number
function al(n){
// k = 13195 is the number that I have to find the prime factor for
var k = 13195;
if (k%n) {
return;
}
else {
document.write(n + ' ');
}
}
</script>
</head>
<body>
<script type="text/javascript">
//a and b are just for counting from the number n to 2 to find the prime numbers
var a = 2;
for (var n = 13194 ; n>a ; n--) {
var b = 2;
//if the found number is divisible, we skip it
while(b<n) {
if (n % b == 0) {
break;
}
else if (b = n - 1){
al(n);
}
b++;
}
}
</script>
</body>
</html>
Now to the problem: This outputs these numbers: "2639 1885 1015 455 377 203 145 91 65 35 29 13 7 5". These aren't prime numbers. I've looked at the numbers and I found that the number 13195 divided by 5 (the last number) gives the first number; 2639; 13195 divided by 7 gives 1885; etc.
What exactly am I doing wrong?
I'm stuck with the JavaScript code I am using for solving a problem which states:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
(this is not homework, is an online coding/mathematical challenge)
So I came up with this solution:
<html>
<head>
<script type="text/javascript">
// This function checks whether it's possible to divide the prime number
function al(n){
// k = 13195 is the number that I have to find the prime factor for
var k = 13195;
if (k%n) {
return;
}
else {
document.write(n + ' ');
}
}
</script>
</head>
<body>
<script type="text/javascript">
//a and b are just for counting from the number n to 2 to find the prime numbers
var a = 2;
for (var n = 13194 ; n>a ; n--) {
var b = 2;
//if the found number is divisible, we skip it
while(b<n) {
if (n % b == 0) {
break;
}
else if (b = n - 1){
al(n);
}
b++;
}
}
</script>
</body>
</html>
Now to the problem: This outputs these numbers: "2639 1885 1015 455 377 203 145 91 65 35 29 13 7 5". These aren't prime numbers. I've looked at the numbers and I found that the number 13195 divided by 5 (the last number) gives the first number; 2639; 13195 divided by 7 gives 1885; etc.
What exactly am I doing wrong?
Share Improve this question edited Jan 22, 2015 at 20:01 user2555451 asked Nov 22, 2011 at 15:09 ahhsadahhsad 291 silver badge6 bronze badges 4- 2 Project Euler kindly asks not to publish solutions to their problems as to prevent people to just copy-paste results. For this reason I am going to edit your title... while it's ok to ask for help with the code, it would be nice if you would not give away for what it is! :) – mac Commented Nov 22, 2011 at 15:33
- 3 I suppose it's kind of lost war though! There are at least 100 more questions with problem solutions having "Project Euler" in the title, and there's even a tag on SO... I just happened to see your particular one! :) – mac Commented Nov 22, 2011 at 15:50
- ahhsad, don't forget to "Accept" an answer. – xan Commented Dec 19, 2011 at 19:07
-
How would I go about applying to any number? For instance, through a
var num = prompt();
– theRyanMark Commented Mar 27, 2014 at 13:03
2 Answers
Reset to default 9Your issue isn't a mathematical one -- you just have a bug in one of your conditional checks.
Change if(b = n-1)
to if(b == n-1)
. Right now, your code isn't actually checking to ensure that a factor is prime; instead, it's assigning the value of n-1
to b
(for odd values of n
) and then automatically calling a1(b
), so your result is all possible odd factors of k
.
My javascript solution-
<script type="text/javascript">
function largestPrimeFactor(number) {
var i = 2;
while (i <= number) {
if (number % i == 0) {
number /= i;
} else {
i++;
}
}
console.log(i);
}
var a = 600851475143 ;
largestPrimeFactor(a)
</script>
本文标签: Prime factor and JavaScriptStack Overflow
版权声明:本文标题:Prime factor and JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741420153a2377755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论