这是我的素数函数,我遇到了问题。
当我通过import Graphics.Element exposing (show)
import Maybe exposing (andThen)
type Tree state = Branch (List (Tree state)) | Leaf state
-- Crumb contains 2 lists:
-- leafs/branches to the left of your focus
-- leafs/branches to the right of your focus
type Crumb state = Crumb (List (Tree state)) (List (Tree state))
type alias Zipper state = (Tree state, List (Crumb state))
tree : Tree String
tree = Branch [Leaf "foo",Leaf "bar",Branch [Leaf "baz",Branch [],Leaf "qux"]]
break : (a -> Bool) -> List a -> (List a, List a)
break p xs = case (List.head xs, List.tail xs) of
(Nothing, Nothing) -> ([], [])
(Just x, Just xs') -> if p x
then ([], xs)
else let (ys,zs) = break p xs'
in (x::ys,zs)
treeInit : Tree state -> Zipper state
treeInit t = (t, [])
treeUp : Zipper state -> Maybe (Zipper state)
treeUp (subtree, bs) = case bs of
[] -> Nothing
Crumb l r::bs' -> Just (Branch <| l++[subtree]++r, bs')
treeTo : state -> Zipper state -> Maybe (Zipper state)
treeTo name node = case node of
(Branch subtrees, bs) ->
let (l, x::r) = break (\(Leaf name') -> name == name') subtrees
in Just (x, Crumb l r::bs)
_ -> Nothing
(!!) : List a -> Int -> Maybe a
xs !! i = case List.tail xs of
Nothing -> Nothing
Just xs' -> if i == 0
then List.head xs
else xs' !! (i-1)
treeToIndex : Int -> Zipper state -> Maybe (Zipper state)
treeToIndex i (Branch subtrees, bs) =
let newTree = subtrees!!i
in case newTree of
Nothing -> Nothing
Just newTree ->
let newCrumb = Crumb (List.take i subtrees) (List.drop (i+1) subtrees)
in Just (newTree, newCrumb::bs)
treeReplace : state -> Zipper state -> Maybe (Zipper state)
treeReplace new node = case node of
(Leaf old, bs) -> Just (Leaf new, bs)
_ -> Nothing
-- the function you're interested in most likely
treeUpdate : (state -> state) -> Zipper state -> Maybe (Zipper state)
treeUpdate f node = case node of
(Leaf name, bs) -> Just (Leaf (f name), bs)
_ -> Nothing
main = (tree |> treeInit |> treeToIndex 2)
`andThen` treeTo "baz" `andThen` treeReplace "xyzzy"
`andThen` treeUp `andThen` treeUp |> show
时,为什么它会返回0
?
2147483647
答案 0 :(得分:4)
正如@ Eugene-Sh所说,计算carre
时会出现溢出。
此外,标准int
至少为16位。然而,大多数编译器现在使其成为32位。所以至少你应该检查一下限制,以便你的代码在所有方面都能很好地完成。
更改为long long
以确保您在carre
和nb
都能获得良好的结果,这样您就可以得到64位。但它只是路径的一半。正如评论中所说,当你超过这个限制时,你仍然会有溢出。
这样的事情,没有改变类型:
int max = INT_MAX; // from limits.h
int maxsqrt = sqrt(INT_MAX); // cast to int, such that maxsqrt² is the max square
if (maxsqrt % 2 == 0)
maxsqrt--; // only odd number considered
int maxcarre = maxsqrt*maxsqrt; // this is the max square (carre in french) for an odd INT
然后在你的循环中,你现在可以检查carre
是否是你可以计算的最后一个,因为maxcarre是最大的一个:
while (carre <= nb)
{
if (nb % i == 0)
return (0);
if (carre >= maxcarre) // this is the last odd square we could compute before overflow
return (-1); // if this -1 represents a "don't" know code for you
carre=carre+4*i+4; // this compute the next square of an odd number
i=i+2;
}
我建议至少使用long
(因此将max更改为LONG_MAX
)以获得更多容量并且仍然在CPU范围内(现代编译器上为64位),甚至long long
,因为至少它在所有编译器上都是64位。
正如@Veltas所说:在某些64位编译器实现上,long是64位。它不依赖于操作系统。此外,IIRC微软的VC ++也在64位上使用32位长。
感谢@Veltas,@ EugeneSh。和其他帮助人们的宝贵反馈!
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
/* As we know 2,3,5,7,11..19....
What is prime number
A prime number (or a prime) is a natural
number greater than 1 that has no positive
divisors other than 1 and itself.
so the formula in if we have n numbers as a input
how we find the prime number ,the formula is
divisible by 2 ...n-1. so 3%2=1 then 3 is a
prime number,10%2=0 so 10 is not a prime number
now 9%2=1 but 9%3=0 that is why we will make a
condition of for loop that divisible 9 with all
the numbers from 2...9-1 and find out whether it
divisible value is 0.
*/
int main()
{
int i,n,j;
bool flag;
printf("Enter the Range Number:");
scanf("%d",&n);
for(i=2;i<=n;i++)
{ bool flag=true;
for(j=2;j<=i-1;j++)
{
if (i%j==0){
flag=false;
break;}
}
if (flag==true)
printf("%d\n",i);
}
return 0;
}