我正在研究用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) 例如,如果我有原子'ABCD,有没有办法确定构成原子的各个字符?
鉴于以下递归函数,神秘(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)
它是否正确?
下面的函数接受一个整数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.
我想将数据从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) 我在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 2.7.1并且我正在尝试安装pygame(2.6)但是当我尝试编译python程序时,我收到以下错误:
ImportError: No module named pygame
有没有办法解决这个问题,而无需安装python 2.6
这是我必须在二叉搜索树中找到第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个最小元素.
这是一个模拟网球被抛出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) 我试图打印从1到100的所有数字,以5结尾.这就是我所拥有的:
for i in range (1, 100):
if (i/10) == 5:
print(i)
Run Code Online (Sandbox Code Playgroud)
为什么打印50?
我已经为 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)