小编kac*_*ous的帖子

Java中的Prime分解程序

我正在研究用Java实现的素数分解程序.目标是找到最大的素数因子600851475143(项目欧拉问题3).我想我已经完成了大部分工作,但是我遇到了一些错误.此外,我的逻辑似乎已关闭,特别是我设置的方法,用于检查数字是否为素数.

public class PrimeFactor {

    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < Math.sqrt(600851475143L); i++) {
            if (Prime(i) && i % Math.sqrt(600851475143L) == 0) {
                count = i;
                System.out.println(count);
            }
        }
    }

    public static boolean Prime(int n) {
        boolean isPrime = false;
        // A number is prime iff it is divisible by 1 and itself only
        if (n % n == 0 && n % 1 == 0) { …
Run Code Online (Sandbox Code Playgroud)

java math primes

2
推荐指数
2
解决办法
3万
查看次数

在Common Lisp(SBCL)中,有没有办法检查原子的各个部分?

例如,如果我有原子'ABCD,有没有办法确定构成原子的各个字符?

sbcl common-lisp

2
推荐指数
1
解决办法
211
查看次数

递归函数

鉴于以下递归函数,神秘(4)会打印什么?

void mysterious(int x) {
    if (x == 0) return;
    printf(“%d ”, x);
    mysterious(x-1);
    mysterious(x-1);
}
Run Code Online (Sandbox Code Playgroud)

这是我的调用堆栈:

mysterious(4) => print 4
mysterious(3) => print 3
mysterious(2) => print 2
mysterious(1) => print 1
mysterious(0) => print 0
Run Code Online (Sandbox Code Playgroud)

它是否正确?

c recursion

1
推荐指数
2
解决办法
547
查看次数

对前n个recriprocals求和的递归函数

下面的函数接受一个整数n并返回前n个倒数的总和.sum(2)应该返回1.5

这是我有的:

 public double sum(int n) {

     if (n < 0) {
       throw new IllegalArgumentException("Illegal Power Argument");
    }

    double zero = 0.0; 

    if(n == 0)
       return zero; 

    else
       return (1/n) + sum(n-1);   
 }
Run Code Online (Sandbox Code Playgroud)

我几乎可以肯定这应该可以工作,但基本上所有东西都返回1.0.

java recursion

1
推荐指数
1
解决办法
3100
查看次数

如何使用fprintf将数据写入文件

我想将数据从C程序写入文件,以便Excel可以读取文件以绘制数据图.但我不确定用于fprintf的确切语法.我在程序的最顶层声明了stdlib.h.我宣布"File*fp;" 在主要但我得到的文件和fp是未申报的.可能是什么问题呢?

**编辑:我的程序编译并运行但现在我的输出文件不包含任何数据这是我在while循环的末尾做了一些计算..

 fp = fopen( "out_file.txt", "w" ); // Open file for writing

 fprintf(fp, "x = %f, y = %f, vx = %f, vy = %f, time = %f, ", x,y,vx,vy,time);
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
5万
查看次数

在python中的简单加法计算器

我在python中构建了一个非常简单的加法计算器:

#This program will add two numbers entered in by the user

print "Welcome!"

num1 = input("Please enter in the first number to be added.")
num2 = input("Please enter in the second number to be added.")

sum = num1 + num2

print "The sum of the two numbers entered is: ", sum
Run Code Online (Sandbox Code Playgroud)

我还没有设置python,所以我使用的是codepad.org(在线编译器).我收到以下错误:欢迎!请输入要加注的第一个号码.Traceback(最近一次呼叫最后一次):第5行,在num1 =输入("请输入第一个要加号的号码.")EOFError

python

0
推荐指数
1
解决办法
7819
查看次数

Pygame安装

我安装了python 2.7.1并且我正在尝试安装pygame(2.6)但是当我尝试编译python程序时,我收到以下错误:
ImportError: No module named pygame

有没有办法解决这个问题,而无需安装python 2.6

python installation pygame failed-installation

0
推荐指数
1
解决办法
3372
查看次数

在BST中查找第k个最小值

这是我必须在二叉搜索树中找到第k个最小值:

struct treeNode 
{
   int data;
   struct treeNode *left, *right:
};

int rank(stuct treeNode* ptr, int k)
{
   if(node == NULL)
    return root; 

   while(ptr->left != NULL) {
     ptr = ptr->left;
     return rank(ptr->left)
   }
}
Run Code Online (Sandbox Code Playgroud)

这显然是不正确的.如果没有提供解决方案,有人可以指导我如何解决这个问题吗?我无法弄清楚如何在BST中找到第k个最小元素.

c binary-search-tree

0
推荐指数
1
解决办法
1430
查看次数

基于小型物理程序的无限循环

这是一个模拟网球被抛出50米建筑物侧面的程序.程序应在每个时间步输出x,y和速度值.但是,我似乎得到了无限循环.

 #include<stdio.h>
 #include<math.h>

 int main() {

     //Intial values 
     float ax = 0; //acceleration in the horizontal direction
     float ay = -9.8; //acceleration in the downward direction
     float x = 0; //top of building at position 0 
     float y = 50; //building is height 50 m
     float vx = 10*cos(30); //velocity in the horizontal direction = 10 m/s * cos(30); 
     float vy = 10*sin(30); //velocity in the vertical direction = 10 m/s * sin(30);     
     int time = 0; //time starts at …
Run Code Online (Sandbox Code Playgroud)

c physics

0
推荐指数
1
解决办法
181
查看次数

找到以5结尾的数字

我试图打印从1到100的所有数字,以5结尾.这就是我所拥有的:

 for i in range (1, 100):
      if (i/10) == 5:
           print(i)
Run Code Online (Sandbox Code Playgroud)

为什么打印50?

python

0
推荐指数
3
解决办法
202
查看次数

欧拉计划问题 4

我已经为 Project Euler 上的问题 4 创建了一个解决方案。但是,我发现将打印语句(打印答案)放在不同位置会打印不同的答案。不知什么原因,结果的最高值是580085。不应该是906609吗?我的 isPalindrome() 方法有问题吗?

 #include <stdio.h>
 #include <stdbool.h>

 int isPalindrome(int n);

 //Find the largest palindrome made from the product of two 3-digit numbers.
 int main(void)
 {    
      int i = 0;
      int j = 0;
      int result = 0;
      int palindrome = 0;
      int max = 0;

      //Each iteration of i will be multiplied from j:10-99
      for(i = 100; i <= 999; i++)
      {
            for(j = 100; j <= 999; j++)
            {
                  result = i * j; …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
1万
查看次数